[llvm-objcopy] Make --set-section-flags work with --add-section

This matches behavior GNU objcopy and can simplify clang-offload-bundler
(which currently works around the issue by invoking llvm-objcopy twice).

Reviewed By: jhenderson

Differential Revision: https://reviews.llvm.org/D90438
This commit is contained in:
Fangrui Song 2020-11-04 09:39:14 -08:00
parent f7649f2d13
commit ee142c4988
3 changed files with 37 additions and 10 deletions

View File

@ -0,0 +1,20 @@
## Check --set-section-flags works with sections added by --add-section.
# RUN: yaml2obj %s -o %t
# RUN: llvm-objcopy --add-section=foo=/dev/null --set-section-flags=foo=alloc,exclude %t %t.out
# RUN: llvm-readobj -S %t.out | FileCheck %s
# CHECK: Name: foo
# CHECK-NEXT: Type: SHT_PROGBITS
# CHECK-NEXT: Flags [
# CHECK-NEXT: SHF_ALLOC
# CHECK-NEXT: SHF_EXCLUDE
# CHECK-NEXT: SHF_WRITE
# CHECK-NEXT: ]
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
Machine: EM_X86_64

View File

@ -18,6 +18,12 @@
# CHECK-NEXT: 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
# CHECK-NEXT: 1: 00000000000004d2 0 NOTYPE GLOBAL DEFAULT ABS newsym
## --set-section-flags works for newly created .symtab, which GNU objcopy does not do.
# RUN: llvm-objcopy -R .symtab --add-symbol newsym=1234 --set-section-flags .symtab=readonly,exclude %t %t3
# RUN: llvm-readelf -S %t3 | FileCheck %s --check-prefix=CHECK2
# CHECK2: [ 4] .symtab SYMTAB 0000000000000000 {{.+}} 000030 18 E 3 1 8
--- !ELF
FileHeader:
Class: ELFCLASS64

View File

@ -719,16 +719,6 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj,
}
}
if (!Config.SetSectionFlags.empty()) {
for (auto &Sec : Obj.sections()) {
const auto Iter = Config.SetSectionFlags.find(Sec.Name);
if (Iter != Config.SetSectionFlags.end()) {
const SectionFlagsUpdate &SFU = Iter->second;
setSectionFlagsAndType(Sec, SFU.NewFlags);
}
}
}
if (Config.OnlyKeepDebug)
for (auto &Sec : Obj.sections())
if (Sec.Flags & SHF_ALLOC && Sec.Type != SHT_NOTE)
@ -770,6 +760,17 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj,
Sec ? (uint16_t)SYMBOL_SIMPLE_INDEX : (uint16_t)SHN_ABS, 0);
}
// --set-section-flags works with sections added by --add-section.
if (!Config.SetSectionFlags.empty()) {
for (auto &Sec : Obj.sections()) {
const auto Iter = Config.SetSectionFlags.find(Sec.Name);
if (Iter != Config.SetSectionFlags.end()) {
const SectionFlagsUpdate &SFU = Iter->second;
setSectionFlagsAndType(Sec, SFU.NewFlags);
}
}
}
if (Config.EntryExpr)
Obj.Entry = Config.EntryExpr(Obj.Entry);
return Error::success();