llvm.org GIT mirror llvm / da1e48a
[llvm-objcopy] Add --prefix-alloc-sections This patch adds support for --prefix-alloc-sections, which adds a prefix to every allocated section names. It adds a prefix after renaming section names by --rename-section as GNU objcopy does. Fixes PR41266: https://bugs.llvm.org/show_bug.cgi?id=41266 Differential Revision: https://reviews.llvm.org/D60042 Patch by Seiya Nuta. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@360233 91177308-0d34-0410-b5e6-96231b3b80d8 James Henderson 5 months ago
8 changed file(s) with 146 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
0 # RUN: llvm-objcopy --prefix-alloc-sections=.prefix %p/Inputs/dynrel.elf %t
1 # RUN: llvm-readobj --sections %t | FileCheck %s
2
3 # CHECK: Name: .prefix.rela.plt
4 # CHECK: Name: .prefix.plt
0 # RUN: yaml2obj %s > %t
1 # RUN: llvm-objcopy --prefix-alloc-sections=.prefix %t %t2
2 # RUN: llvm-readobj --sections %t2 | FileCheck %s
3
4 !ELF
5 FileHeader:
6 Class: ELFCLASS64
7 Data: ELFDATA2LSB
8 Type: ET_REL
9 Machine: EM_X86_64
10 Sections:
11 - Name: .foo
12 Type: SHT_PROGBITS
13 Flags: [ ]
14 - Name: .prefix.already_prefixed
15 Type: SHT_PROGBITS
16 Flags: [ SHF_ALLOC ]
17 - Name: .text
18 Type: SHT_PROGBITS
19 Flags: [ SHF_ALLOC ]
20 - Name: .rel.text
21 Type: SHT_REL
22 Info: .text
23 - Name: .rela.data
24 Type: SHT_RELA
25 Info: .data
26 - Name: .data
27 Type: SHT_PROGBITS
28 Flags: [ SHF_ALLOC ]
29
30 # CHECK: Name: .foo
31 # CHECK: Name: .prefix.prefix.already_prefixed
32 # CHECK: Name: .prefix.text
33 # CHECK: Name: .rel.prefix.text
34 # CHECK: Name: .rela.prefix.data
35 # CHECK: Name: .prefix.data
0 # RUN: yaml2obj %s > %t
1 # RUN: llvm-objcopy --rename-section=.text=.text2 --rename-section=.data=.data2 --prefix-alloc-sections=.prefix %t %t2
2 # RUN: llvm-readobj --sections %t2 | FileCheck %s
3
4 # .text/.rel.text and .data/.rela.data are the cases when the relocation section
5 # comes after/before its target section respectively. We handle them in different
6 # ways to perform both --rename-section and --prefix-alloc-sections in one pass.
7
8 !ELF
9 FileHeader:
10 Class: ELFCLASS64
11 Data: ELFDATA2LSB
12 Type: ET_REL
13 Machine: EM_X86_64
14 Sections:
15 - Name: .text
16 Type: SHT_PROGBITS
17 Flags: [ SHF_ALLOC ]
18 - Name: .rel.text
19 Type: SHT_REL
20 Info: .text
21 - Name: .rela.data
22 Type: SHT_RELA
23 Info: .data
24 - Name: .data
25 Type: SHT_PROGBITS
26 Flags: [ SHF_ALLOC ]
27
28 # CHECK: Name: .prefix.text2
29 # CHECK: Name: .rel.prefix.text2
30 # CHECK: Name: .rela.prefix.data2
31 # CHECK: Name: .prefix.data2
176176 if (Config.AllowBrokenLinks || !Config.BuildIdLinkDir.empty() ||
177177 Config.BuildIdLinkInput || Config.BuildIdLinkOutput ||
178178 !Config.SplitDWO.empty() || !Config.SymbolsPrefix.empty() ||
179 !Config.AddSection.empty() || !Config.DumpSection.empty() ||
180 !Config.KeepSection.empty() || !Config.SymbolsToGlobalize.empty() ||
181 !Config.SymbolsToKeep.empty() || !Config.SymbolsToLocalize.empty() ||
182 !Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() ||
183 !Config.SectionsToRename.empty() || !Config.SetSectionFlags.empty() ||
184 !Config.SymbolsToRename.empty() || Config.ExtractDWO ||
185 Config.KeepFileSymbols || Config.LocalizeHidden || Config.PreserveDates ||
186 Config.StripDWO || Config.StripNonAlloc || Config.StripSections ||
187 Config.Weaken || Config.DecompressDebugSections ||
179 !Config.AllocSectionsPrefix.empty() || !Config.AddSection.empty() ||
180 !Config.DumpSection.empty() || !Config.KeepSection.empty() ||
181 !Config.SymbolsToGlobalize.empty() || !Config.SymbolsToKeep.empty() ||
182 !Config.SymbolsToLocalize.empty() || !Config.SymbolsToWeaken.empty() ||
183 !Config.SymbolsToKeepGlobal.empty() || !Config.SectionsToRename.empty() ||
184 !Config.SetSectionFlags.empty() || !Config.SymbolsToRename.empty() ||
185 Config.ExtractDWO || Config.KeepFileSymbols || Config.LocalizeHidden ||
186 Config.PreserveDates || Config.StripDWO || Config.StripNonAlloc ||
187 Config.StripSections || Config.Weaken || Config.DecompressDebugSections ||
188188 Config.DiscardMode == DiscardType::Locals ||
189189 !Config.SymbolsToAdd.empty() || Config.EntryExpr) {
190190 return createStringError(llvm::errc::invalid_argument,
498498 InputArgs.getLastArgValue(OBJCOPY_build_id_link_output);
499499 Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
500500 Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols);
501 Config.AllocSectionsPrefix =
502 InputArgs.getLastArgValue(OBJCOPY_prefix_alloc_sections);
501503
502504 for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) {
503505 if (!StringRef(Arg->getValue()).contains('='))
119119 Optional BuildIdLinkOutput;
120120 StringRef SplitDWO;
121121 StringRef SymbolsPrefix;
122 StringRef AllocSectionsPrefix;
122123 DiscardType DiscardMode = DiscardType::None;
123124
124125 // Repeated options
1212 #include "llvm-objcopy.h"
1313
1414 #include "llvm/ADT/BitmaskEnum.h"
15 #include "llvm/ADT/DenseSet.h"
1516 #include "llvm/ADT/Optional.h"
1617 #include "llvm/ADT/STLExtras.h"
1718 #include "llvm/ADT/SmallVector.h"
579580 if (Error E = updateAndRemoveSymbols(Config, Obj))
580581 return E;
581582
582 if (!Config.SectionsToRename.empty()) {
583 if (!Config.SectionsToRename.empty() || !Config.AllocSectionsPrefix.empty()) {
584 DenseSet PrefixedSections;
583585 for (auto &Sec : Obj.sections()) {
584586 const auto Iter = Config.SectionsToRename.find(Sec.Name);
585587 if (Iter != Config.SectionsToRename.end()) {
587589 Sec.Name = SR.NewName;
588590 if (SR.NewFlags.hasValue())
589591 setSectionFlagsAndType(Sec, SR.NewFlags.getValue());
592 }
593
594 // Add a prefix to allocated sections and their relocation sections. This
595 // should be done after renaming the section by Config.SectionToRename to
596 // imitate the GNU objcopy behavior.
597 if (!Config.AllocSectionsPrefix.empty()) {
598 if (Sec.Flags & SHF_ALLOC) {
599 Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str();
600 PrefixedSections.insert(&Sec);
601
602 // Rename relocation sections associated to the allocated sections.
603 // For example, if we rename .text to .prefix.text, we also rename
604 // .rel.text to .rel.prefix.text.
605 //
606 // Dynamic relocation sections (SHT_REL[A] with SHF_ALLOC) are handled
607 // above, e.g., .rela.plt is renamed to .prefix.rela.plt, not
608 // .rela.prefix.plt since GNU objcopy does so.
609 } else if (auto *RelocSec = dyn_cast(&Sec)) {
610 auto *TargetSec = RelocSec->getSection();
611 if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) {
612 StringRef prefix;
613 switch (Sec.Type) {
614 case SHT_REL:
615 prefix = ".rel";
616 break;
617 case SHT_RELA:
618 prefix = ".rela";
619 break;
620 default:
621 continue;
622 }
623
624 // If the relocation section comes *after* the target section, we
625 // don't add Config.AllocSectionsPrefix because we've already added
626 // the prefix to TargetSec->Name. Otherwise, if the relocation
627 // section comes *before* the target section, we add the prefix.
628 if (PrefixedSections.count(TargetSec)) {
629 Sec.Name = (prefix + TargetSec->Name).str();
630 } else {
631 const auto Iter = Config.SectionsToRename.find(TargetSec->Name);
632 if (Iter != Config.SectionsToRename.end()) {
633 // Both `--rename-section` and `--prefix-alloc-sections` are
634 // given but the target section is not yet renamed.
635 Sec.Name =
636 (prefix + Config.AllocSectionsPrefix + Iter->second.NewName)
637 .str();
638 } else {
639 Sec.Name =
640 (prefix + Config.AllocSectionsPrefix + TargetSec->Name)
641 .str();
642 }
643 }
644 }
645 }
590646 }
591647 }
592648 }
237237 : Eq<"prefix-symbols", "Add to the start of every symbol name">,
238238 MetaVarName<"prefix">;
239239
240 defm prefix_alloc_sections
241 : Eq<"prefix-alloc-sections", "Add to the start of every allocated section name">,
242 MetaVarName<"prefix">;
243
240244 def version : Flag<["--"], "version">,
241245 HelpText<"Print the version and exit.">;
242246 def V : Flag<["-"], "V">, Alias;