[ELF][Objcopy] unable to locate the extended symbol index table after update-section
Problem:
I'm trying to insert some data into my ELF file. To achieve this, I create a const char array with the attribute __attribute__((section(".rodata.holder"))). Then, I try to insert the data into the object file using llvm-objcopy --update-section. However, during the linking process, I get an error from the linker:
ld.lld: error: found an extended symbol index (5), but unable to locate the extended symbol index table
How to reproduce:
Step 1 : Download and build llvm 21.1.5 tools
git clone https://github.com/llvm/llvm-project.git && cd llvm-project && git checkout llvmorg-21.1.5
mkdir bld && cd bld
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_ENABLE_PROJECTS="llvm;clang;lld" ../llvm
ninja clang lld llvm-objcopy
Step 2 : Put broken sources into build directory
Source examples:
// main.cpp
int External(const char*, const char*, const char*) { return 0; }
int main() { return 0; }
//lib.cpp
static __attribute__((section(".rodata.holder"))) const char resource_holder[0x1000] = {0};
int External(const char*, const char*, const char*);
static const int REG1 = External("Range1", resource_holder + 128 * 0, resource_holder + 128 * 1);
static const int REG2 = External("Range2", resource_holder + 128 * 1, resource_holder + 128 * 2);
static const int REG3 = External("Range3", resource_holder + 128 * 2, resource_holder + 128 * 3);
Generate some random binary data:
head -c 4k /dev/urandom > data.bin
Step 3 : Try to compile
./bin/clang++ lib.cpp -c -O0 -g0 -o lib.o
./bin/llvm-objcopy lib.o --update-section=.rodata.holder=data.bin
./bin/clang++ lib.o main.cpp -o a.out -fuse-ld=lld --ld-path=./bin/ld.lld
# ld.lld: error: found an extended symbol index (5), but unable to locate the extended symbol index table
Possible bug(?) explanation
I've spent some time debugging. It seems the problem is related to the code of the Object::updateSectionData function:
https://github.com/llvm/llvm-project/blob/3b83e7fa4ec18991a25d49741418e2b3d325692c/llvm/lib/ObjCopy/ELF/ELFObject.cpp#L2160-L2182
If we create a new section, then there will not be any calls to Object::replaceSections:
https://github.com/llvm/llvm-project/blob/3b83e7fa4ec18991a25d49741418e2b3d325692c/llvm/lib/ObjCopy/ELF/ELFObject.cpp#L2173-L2179
So, the symbols were not updated with the appropriate section index and an error occurred.
Could anyone tell me is it correct behaviour or we should fix it?
@llvm/issue-subscribers-tools-llvm-objcopy-strip
Author: Dmitry Nechitaev (Nechda)
ld.lld: error: found an extended symbol index (5), but unable to locate the extended symbol index table
How to reproduce:
Step 1 : Download and build llvm 21.1.5 tools
git clone https://github.com/llvm/llvm-project.git && cd llvm-project && git checkout llvmorg-21.1.5
mkdir bld && cd bld
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_ENABLE_PROJECTS="llvm;clang;lld" ../llvm
ninja clang lld llvm-objcopy
Step 2 : Put broken sources into build directory
Source examples:
// main.cpp
int External(const char*, const char*, const char*) { return 0; }
int main() { return 0; }
//lib.cpp
static __attribute__((section(".rodata.holder"))) const char resource_holder[0x1000] = {0};
int External(const char*, const char*, const char*);
static const int REG1 = External("Range1", resource_holder + 128 * 0, resource_holder + 128 * 1);
static const int REG2 = External("Range2", resource_holder + 128 * 1, resource_holder + 128 * 2);
static const int REG3 = External("Range3", resource_holder + 128 * 2, resource_holder + 128 * 3);
Generate some random binary data:
head -c 4k /dev/urandom > data.bin
Step 3 : Try to compile
./bin/clang++ lib.cpp -c -O0 -g0 -o lib.o
./bin/llvm-objcopy lib.o --update-section=.rodata.holder=data.bin
./bin/clang++ lib.o main.cpp -o a.out -fuse-ld=lld --ld-path=./bin/ld.lld
# ld.lld: error: found an extended symbol index (5), but unable to locate the extended symbol index table
Possible bug(?) explanation
I've spent some time debugging. It seems the problem is related to the code of the Object::updateSectionData function:
https://github.com/llvm/llvm-project/blob/3b83e7fa4ec18991a25d49741418e2b3d325692c/llvm/lib/ObjCopy/ELF/ELFObject.cpp#L2160-L2182
If we create a new section, then there will not be any calls to Object::replaceSections:
https://github.com/llvm/llvm-project/blob/3b83e7fa4ec18991a25d49741418e2b3d325692c/llvm/lib/ObjCopy/ELF/ELFObject.cpp#L2173-L2179
So, the symbols were not updated with the appropriate section index and an error occurred.
Could anyone tell me is it correct behaviour or we should fix it?
I'll take a look into the error you're seeing, as it doesn't sound like it should be happening. That being said, it seems like you'd be better off using -I binary (see https://llvm.org/docs/CommandGuide/llvm-objcopy.html#binary-input-and-output) to turn your data into an object file with symbols that you can reference from the rest of your code.
Thank you for the information about the -I binary option in llvm-objcopy! In the past, I used a sequence of commands such as --add-section and --add-symbol to achieve the same result.
However, the real task I'm trying to solve is more complex, and the proposed flag is not working on Darwin-like platforms (there is no Mach-O value for the supported output formats). The piece of code I described in the issue description was not expected to cause any errors during linking, and I am prepared to fix it for ELF files.
Not sure I'm following your reference to Darwin-like platforms. llvm-objcopy has completely different code paths for ELF and Mach-O, so any issues with one are unrelated to the behaviour of the other.
Anyway, it doesn't matter. I've gone over the code a bit more and there's definitely a bug here, due to a dangling reference. Specifically, the line of code at https://github.com/llvm/llvm-project/blob/3b83e7fa4ec18991a25d49741418e2b3d325692c/llvm/lib/ObjCopy/ELF/ELFObject.cpp#L2174 seems suspicious. This is replacing the existing .rodata.holder SectionBase instance, which ultimately results in that object being deleted. Subsequently, when writing out the symbol table, we hit this line of code to get the section a symbol is defined in. The Symbol here has DefinedIn pointing at the now-deleted SectionBase instance, which means it's garbage and the subsequent code is unlikely to do the right thing.
I suspect, but have not confirmed, that there may be other symptoms of the same issue, e.g. if the updated section was a member of a section group, I suspect the contents of the associated group might get corrupted. I haven't attempted to audit all such situations.
Whether there are other issues or not, I think the correct thing to do is either call replaceSections instead of the naive assignment done in updateSectionData, or possibly just do the replaceSectionReferences loop that is currently only done in replaceSections. I haven't attempted to determine which is the right approach.
@Nechda Hi, thanks for reporting and documenting this issue which matches a problem we are also running into.
I saw that you already identified the root cause in this issue. May I ask:
1. Has this been fixed upstream, or is a patch currently being worked on? 2. Is there any known workaround using the current llvm-objcopy (single-pass or multi-pass)? 3. If you have a patched local build or a recommended sequence of objcopy commands that avoids the incorrect symbol type, could you share the details?
Any update would be greatly appreciated — it would help us confirm whether we should wait for a fix, apply a patch, or re-design our embedding pipeline.
Thanks again for opening this issue and for any information you can provide!
@img2col, FYI the issue hasn't been fixed in current LLVM. For a workaround, you'd need to describe more what you are doing for me to be able to provide a valid workaround for your issue.
I am currently working on creating a PR with a fix, but I am having some difficulty figuring out how to create a minimal test case that can reproduce the issue (mostly in assembly language).
I'm having trouble understanding why this error occurs in some cases and not in others. As a result, I can't select the best way to detect the problem. It seems that I should run my tests under a memory-sanitized llvm-objcopy.
Hi @Nechda,
I suspect you don't want to use assembly for the test input, but rather are better off using YAML and yaml2obj, like most llvm-objcopy tests. All you need is a single section and symbol, something like the following:
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_REL
Sections:
- Name: .foo
Type: SHT_PROGBITS
Size: 0x10
Symbols:
- Name: foo
Type: STT_FUNC
Value: 0x1
Section: .foo
If you then use llvm-objcopy --update-section to replace .foo with something, then use llvm-readelf to dump the symbols of the output file, you'll see the symbol index for foo has been corrupted.
@jh7370 Thank you for your help in providing a simple test case!
To reproduce the error in this example, I build llvm-objcopy with address sanitizer. If I run llvm-objcopy without sanitizing, the error does not occur. So, the PR created implements a unit test that I hope will run under address sanitizers in CI.