llvm.org GIT mirror llvm / 416cb77
[llvm-objcopy] Use SHT_NOTE for added note sections. Summary: Fix llvm-objcopy to add .note sections as SHT_NOTEs. GNU objcopy overrides section flags for special sections. For `.note` sections (with the exception of `.note.GNU-stack`), SHT_NOTE is used. Many other sections are special cased by libbfd, but `.note` is the only special section I can seem to find being used with objcopy --add-section. See `.note` in context of the full list of special sections here: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=blob;f=bfd/elf.c;h=eb3e1828e9c651678b95a1dcbc3b124783d1d2be;hb=HEAD#l2675 Reviewers: jhenderson, alexshap, jakehehrlich, espindola Reviewed By: jhenderson Subscribers: emaste, arichardson, llvm-commits Differential Revision: https://reviews.llvm.org/D56570 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351204 91177308-0d34-0410-b5e6-96231b3b80d8 Jordan Rupprecht 7 months ago
3 changed file(s) with 71 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
0 # Verify that --add-section can be used to add a note section which is
1 # successfully interpreted by tools that read notes.
2
3 # Add [namesz, descsz, type, name, desc] for a build id.
4 # RUN: echo -e -n "\x04\x00\x00\x00" > %t-note.bin
5 # RUN: echo -e -n "\x10\x00\x00\x00" >> %t-note.bin
6 # RUN: echo -e -n "\x03\x00\x00\x00" >> %t-note.bin
7 # RUN: echo -e -n "GNU\x00" >> %t-note.bin
8 # RUN: echo -e -n "\x00\x01\x02\x03" >> %t-note.bin
9 # RUN: echo -e -n "\x04\x05\x06\x07" >> %t-note.bin
10 # RUN: echo -e -n "\x08\x09\x0a\x0b" >> %t-note.bin
11 # RUN: echo -e -n "\x0c\x0d\x0e\x0f" >> %t-note.bin
12
13 # RUN: yaml2obj %s > %t.o
14 # RUN: llvm-objcopy --add-section=.note.gnu.build-id=%t-note.bin %t.o %t-with-note.o
15 # RUN: llvm-readobj --notes %t-with-note.o | FileCheck %s
16
17 !ELF
18 FileHeader:
19 Class: ELFCLASS64
20 Data: ELFDATA2LSB
21 Type: ET_REL
22 Machine: EM_X86_64
23
24 # CHECK: Notes [
25 # CHECK-NEXT: NoteSection {
26 # CHECK-NEXT: Offset:
27 # CHECK-NEXT: Size:
28 # CHECK-NEXT: Note {
29 # CHECK-NEXT: Owner: GNU
30 # CHECK-NEXT: Data size: 0x10
31 # CHECK-NEXT: Type: NT_GNU_BUILD_ID
32 # CHECK-NEXT: Build ID: 000102030405060708090a0b0c0d0e0f
33 # CHECK-NEXT: }
34 # CHECK-NEXT: }
35 # CHECK-NEXT: ]
0 # Check the properties of added sections.
1 # By default, sections are SHT_PROGBITS, but .note sections (excluding
2 # .note.GNU-stack) are SHT_NOTE sections.
3
4 # RUN: yaml2obj %s > %t.o
5 # RUN: llvm-objcopy --add-section=.foo=/dev/null %t.o %t-foo.o
6 # RUN: llvm-objcopy --add-section=.note.foo=/dev/null %t.o %t-regular-note.o
7 # RUN: llvm-objcopy --add-section=.note.GNU-stack=/dev/null %t.o %t-gnu-stack.o
8 # RUN: llvm-readelf --sections %t-foo.o | FileCheck %s --check-prefix=NORMAL
9 # RUN: llvm-readelf --sections %t-regular-note.o | FileCheck %s --check-prefix=NOTE
10 # RUN: llvm-readelf --sections %t-gnu-stack.o | FileCheck %s --check-prefix=GNU-STACK
11
12 !ELF
13 FileHeader:
14 Class: ELFCLASS64
15 Data: ELFDATA2LSB
16 Type: ET_REL
17 Machine: EM_X86_64
18
19 # NORMAL: .foo PROGBITS
20 # NOTE: .note.foo NOTE
21 # GNU-STACK: .note.GNU-stack PROGBITS
498498
499499 if (!Config.AddSection.empty()) {
500500 for (const auto &Flag : Config.AddSection) {
501 auto SecPair = Flag.split("=");
502 auto SecName = SecPair.first;
503 auto File = SecPair.second;
504 auto BufOrErr = MemoryBuffer::getFile(File);
501 std::pair SecPair = Flag.split("=");
502 StringRef SecName = SecPair.first;
503 StringRef File = SecPair.second;
504 ErrorOr> BufOrErr =
505 MemoryBuffer::getFile(File);
505506 if (!BufOrErr)
506507 reportError(File, BufOrErr.getError());
507 auto Buf = std::move(*BufOrErr);
508 auto BufPtr = reinterpret_cast(Buf->getBufferStart());
509 auto BufSize = Buf->getBufferSize();
510 Obj.addSection(SecName,
511 ArrayRef(BufPtr, BufSize));
508 std::unique_ptr Buf = std::move(*BufOrErr);
509 ArrayRef Data(
510 reinterpret_cast(Buf->getBufferStart()),
511 Buf->getBufferSize());
512 OwnedDataSection &NewSection =
513 Obj.addSection(SecName, Data);
514 if (SecName.startswith(".note") && SecName != ".note.GNU-stack")
515 NewSection.Type = SHT_NOTE;
512516 }
513517 }
514518