xref: /freebsd/contrib/llvm-project/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 //===- ELFObjcopy.cpp -----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ObjCopy/ELF/ELFObjcopy.h"
10 #include "ELFObject.h"
11 #include "llvm/ADT/BitmaskEnum.h"
12 #include "llvm/ADT/DenseSet.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/BinaryFormat/ELF.h"
18 #include "llvm/MC/MCTargetOptions.h"
19 #include "llvm/ObjCopy/CommonConfig.h"
20 #include "llvm/ObjCopy/ELF/ELFConfig.h"
21 #include "llvm/Object/Binary.h"
22 #include "llvm/Object/ELFObjectFile.h"
23 #include "llvm/Object/ELFTypes.h"
24 #include "llvm/Object/Error.h"
25 #include "llvm/Option/Option.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/Compression.h"
28 #include "llvm/Support/Errc.h"
29 #include "llvm/Support/Error.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/ErrorOr.h"
32 #include "llvm/Support/FileSystem.h"
33 #include "llvm/Support/Memory.h"
34 #include "llvm/Support/Path.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include <algorithm>
37 #include <cassert>
38 #include <cstdlib>
39 #include <functional>
40 #include <iterator>
41 #include <memory>
42 #include <string>
43 #include <system_error>
44 #include <utility>
45 
46 using namespace llvm;
47 using namespace llvm::ELF;
48 using namespace llvm::objcopy;
49 using namespace llvm::objcopy::elf;
50 using namespace llvm::object;
51 
52 using SectionPred = std::function<bool(const SectionBase &Sec)>;
53 
54 static bool isDebugSection(const SectionBase &Sec) {
55   return StringRef(Sec.Name).starts_with(".debug") || Sec.Name == ".gdb_index";
56 }
57 
58 static bool isDWOSection(const SectionBase &Sec) {
59   return StringRef(Sec.Name).ends_with(".dwo");
60 }
61 
62 static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
63   // We can't remove the section header string table.
64   if (&Sec == Obj.SectionNames)
65     return false;
66   // Short of keeping the string table we want to keep everything that is a DWO
67   // section and remove everything else.
68   return !isDWOSection(Sec);
69 }
70 
71 static Expected<uint64_t> getNewShfFlags(SectionFlag AllFlags,
72                                          uint16_t EMachine) {
73   uint64_t NewFlags = 0;
74   if (AllFlags & SectionFlag::SecAlloc)
75     NewFlags |= ELF::SHF_ALLOC;
76   if (!(AllFlags & SectionFlag::SecReadonly))
77     NewFlags |= ELF::SHF_WRITE;
78   if (AllFlags & SectionFlag::SecCode)
79     NewFlags |= ELF::SHF_EXECINSTR;
80   if (AllFlags & SectionFlag::SecMerge)
81     NewFlags |= ELF::SHF_MERGE;
82   if (AllFlags & SectionFlag::SecStrings)
83     NewFlags |= ELF::SHF_STRINGS;
84   if (AllFlags & SectionFlag::SecExclude)
85     NewFlags |= ELF::SHF_EXCLUDE;
86   if (AllFlags & SectionFlag::SecLarge) {
87     if (EMachine != EM_X86_64)
88       return createStringError(errc::invalid_argument,
89                                "section flag SHF_X86_64_LARGE can only be used "
90                                "with x86_64 architecture");
91     NewFlags |= ELF::SHF_X86_64_LARGE;
92   }
93   return NewFlags;
94 }
95 
96 static uint64_t getSectionFlagsPreserveMask(uint64_t OldFlags,
97                                             uint64_t NewFlags,
98                                             uint16_t EMachine) {
99   // Preserve some flags which should not be dropped when setting flags.
100   // Also, preserve anything OS/processor dependant.
101   const uint64_t PreserveMask =
102       (ELF::SHF_COMPRESSED | ELF::SHF_GROUP | ELF::SHF_LINK_ORDER |
103        ELF::SHF_MASKOS | ELF::SHF_MASKPROC | ELF::SHF_TLS |
104        ELF::SHF_INFO_LINK) &
105       ~ELF::SHF_EXCLUDE &
106       ~(EMachine == EM_X86_64 ? (uint64_t)ELF::SHF_X86_64_LARGE : 0UL);
107   return (OldFlags & PreserveMask) | (NewFlags & ~PreserveMask);
108 }
109 
110 static void setSectionType(SectionBase &Sec, uint64_t Type) {
111   // If Sec's type is changed from SHT_NOBITS due to --set-section-flags,
112   // Offset may not be aligned. Align it to max(Align, 1).
113   if (Sec.Type == ELF::SHT_NOBITS && Type != ELF::SHT_NOBITS)
114     Sec.Offset = alignTo(Sec.Offset, std::max(Sec.Align, uint64_t(1)));
115   Sec.Type = Type;
116 }
117 
118 static Error setSectionFlagsAndType(SectionBase &Sec, SectionFlag Flags,
119                                     uint16_t EMachine) {
120   Expected<uint64_t> NewFlags = getNewShfFlags(Flags, EMachine);
121   if (!NewFlags)
122     return NewFlags.takeError();
123   Sec.Flags = getSectionFlagsPreserveMask(Sec.Flags, *NewFlags, EMachine);
124 
125   // In GNU objcopy, certain flags promote SHT_NOBITS to SHT_PROGBITS. This rule
126   // may promote more non-ALLOC sections than GNU objcopy, but it is fine as
127   // non-ALLOC SHT_NOBITS sections do not make much sense.
128   if (Sec.Type == SHT_NOBITS &&
129       (!(Sec.Flags & ELF::SHF_ALLOC) ||
130        Flags & (SectionFlag::SecContents | SectionFlag::SecLoad)))
131     setSectionType(Sec, ELF::SHT_PROGBITS);
132 
133   return Error::success();
134 }
135 
136 static ElfType getOutputElfType(const Binary &Bin) {
137   // Infer output ELF type from the input ELF object
138   if (isa<ELFObjectFile<ELF32LE>>(Bin))
139     return ELFT_ELF32LE;
140   if (isa<ELFObjectFile<ELF64LE>>(Bin))
141     return ELFT_ELF64LE;
142   if (isa<ELFObjectFile<ELF32BE>>(Bin))
143     return ELFT_ELF32BE;
144   if (isa<ELFObjectFile<ELF64BE>>(Bin))
145     return ELFT_ELF64BE;
146   llvm_unreachable("Invalid ELFType");
147 }
148 
149 static ElfType getOutputElfType(const MachineInfo &MI) {
150   // Infer output ELF type from the binary arch specified
151   if (MI.Is64Bit)
152     return MI.IsLittleEndian ? ELFT_ELF64LE : ELFT_ELF64BE;
153   else
154     return MI.IsLittleEndian ? ELFT_ELF32LE : ELFT_ELF32BE;
155 }
156 
157 static std::unique_ptr<Writer> createELFWriter(const CommonConfig &Config,
158                                                Object &Obj, raw_ostream &Out,
159                                                ElfType OutputElfType) {
160   // Depending on the initial ELFT and OutputFormat we need a different Writer.
161   switch (OutputElfType) {
162   case ELFT_ELF32LE:
163     return std::make_unique<ELFWriter<ELF32LE>>(Obj, Out, !Config.StripSections,
164                                                 Config.OnlyKeepDebug);
165   case ELFT_ELF64LE:
166     return std::make_unique<ELFWriter<ELF64LE>>(Obj, Out, !Config.StripSections,
167                                                 Config.OnlyKeepDebug);
168   case ELFT_ELF32BE:
169     return std::make_unique<ELFWriter<ELF32BE>>(Obj, Out, !Config.StripSections,
170                                                 Config.OnlyKeepDebug);
171   case ELFT_ELF64BE:
172     return std::make_unique<ELFWriter<ELF64BE>>(Obj, Out, !Config.StripSections,
173                                                 Config.OnlyKeepDebug);
174   }
175   llvm_unreachable("Invalid output format");
176 }
177 
178 static std::unique_ptr<Writer> createWriter(const CommonConfig &Config,
179                                             Object &Obj, raw_ostream &Out,
180                                             ElfType OutputElfType) {
181   switch (Config.OutputFormat) {
182   case FileFormat::Binary:
183     return std::make_unique<BinaryWriter>(Obj, Out, Config);
184   case FileFormat::IHex:
185     return std::make_unique<IHexWriter>(Obj, Out, Config.OutputFilename);
186   case FileFormat::SREC:
187     return std::make_unique<SRECWriter>(Obj, Out, Config.OutputFilename);
188   default:
189     return createELFWriter(Config, Obj, Out, OutputElfType);
190   }
191 }
192 
193 static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
194                                Object &Obj) {
195   for (auto &Sec : Obj.sections()) {
196     if (Sec.Name == SecName) {
197       if (Sec.Type == SHT_NOBITS)
198         return createStringError(object_error::parse_failed,
199                                  "cannot dump section '%s': it has no contents",
200                                  SecName.str().c_str());
201       Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
202           FileOutputBuffer::create(Filename, Sec.OriginalData.size());
203       if (!BufferOrErr)
204         return BufferOrErr.takeError();
205       std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
206       std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
207                 Buf->getBufferStart());
208       if (Error E = Buf->commit())
209         return E;
210       return Error::success();
211     }
212   }
213   return createStringError(object_error::parse_failed, "section '%s' not found",
214                            SecName.str().c_str());
215 }
216 
217 Error Object::compressOrDecompressSections(const CommonConfig &Config) {
218   // Build a list of sections we are going to replace.
219   // We can't call `addSection` while iterating over sections,
220   // because it would mutate the sections array.
221   SmallVector<std::pair<SectionBase *, std::function<SectionBase *()>>, 0>
222       ToReplace;
223   for (SectionBase &Sec : sections()) {
224     std::optional<DebugCompressionType> CType;
225     for (auto &[Matcher, T] : Config.compressSections)
226       if (Matcher.matches(Sec.Name))
227         CType = T;
228     // Handle --compress-debug-sections and --decompress-debug-sections, which
229     // apply to non-ALLOC debug sections.
230     if (!(Sec.Flags & SHF_ALLOC) && StringRef(Sec.Name).starts_with(".debug")) {
231       if (Config.CompressionType != DebugCompressionType::None)
232         CType = Config.CompressionType;
233       else if (Config.DecompressDebugSections)
234         CType = DebugCompressionType::None;
235     }
236     if (!CType)
237       continue;
238 
239     if (Sec.ParentSegment)
240       return createStringError(
241           errc::invalid_argument,
242           "section '" + Sec.Name +
243               "' within a segment cannot be (de)compressed");
244 
245     if (auto *CS = dyn_cast<CompressedSection>(&Sec)) {
246       if (*CType == DebugCompressionType::None)
247         ToReplace.emplace_back(
248             &Sec, [=] { return &addSection<DecompressedSection>(*CS); });
249     } else if (*CType != DebugCompressionType::None) {
250       ToReplace.emplace_back(&Sec, [=, S = &Sec] {
251         return &addSection<CompressedSection>(
252             CompressedSection(*S, *CType, Is64Bits));
253       });
254     }
255   }
256 
257   DenseMap<SectionBase *, SectionBase *> FromTo;
258   for (auto [S, Func] : ToReplace)
259     FromTo[S] = Func();
260   return replaceSections(FromTo);
261 }
262 
263 static bool isAArch64MappingSymbol(const Symbol &Sym) {
264   if (Sym.Binding != STB_LOCAL || Sym.Type != STT_NOTYPE ||
265       Sym.getShndx() == SHN_UNDEF)
266     return false;
267   StringRef Name = Sym.Name;
268   if (!Name.consume_front("$x") && !Name.consume_front("$d"))
269     return false;
270   return Name.empty() || Name.starts_with(".");
271 }
272 
273 static bool isArmMappingSymbol(const Symbol &Sym) {
274   if (Sym.Binding != STB_LOCAL || Sym.Type != STT_NOTYPE ||
275       Sym.getShndx() == SHN_UNDEF)
276     return false;
277   StringRef Name = Sym.Name;
278   if (!Name.consume_front("$a") && !Name.consume_front("$d") &&
279       !Name.consume_front("$t"))
280     return false;
281   return Name.empty() || Name.starts_with(".");
282 }
283 
284 // Check if the symbol should be preserved because it is required by ABI.
285 static bool isRequiredByABISymbol(const Object &Obj, const Symbol &Sym) {
286   switch (Obj.Machine) {
287   case EM_AARCH64:
288     // Mapping symbols should be preserved for a relocatable object file.
289     return Obj.isRelocatable() && isAArch64MappingSymbol(Sym);
290   case EM_ARM:
291     // Mapping symbols should be preserved for a relocatable object file.
292     return Obj.isRelocatable() && isArmMappingSymbol(Sym);
293   default:
294     return false;
295   }
296 }
297 
298 static bool isUnneededSymbol(const Symbol &Sym) {
299   return !Sym.Referenced &&
300          (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) &&
301          Sym.Type != STT_SECTION;
302 }
303 
304 static Error updateAndRemoveSymbols(const CommonConfig &Config,
305                                     const ELFConfig &ELFConfig, Object &Obj) {
306   // TODO: update or remove symbols only if there is an option that affects
307   // them.
308   if (!Obj.SymbolTable)
309     return Error::success();
310 
311   Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
312     if (Config.SymbolsToSkip.matches(Sym.Name))
313       return;
314 
315     // Common and undefined symbols don't make sense as local symbols, and can
316     // even cause crashes if we localize those, so skip them.
317     if (!Sym.isCommon() && Sym.getShndx() != SHN_UNDEF &&
318         ((ELFConfig.LocalizeHidden &&
319           (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
320          Config.SymbolsToLocalize.matches(Sym.Name)))
321       Sym.Binding = STB_LOCAL;
322 
323     for (auto &[Matcher, Visibility] : ELFConfig.SymbolsToSetVisibility)
324       if (Matcher.matches(Sym.Name))
325         Sym.Visibility = Visibility;
326 
327     // Note: these two globalize flags have very similar names but different
328     // meanings:
329     //
330     // --globalize-symbol: promote a symbol to global
331     // --keep-global-symbol: all symbols except for these should be made local
332     //
333     // If --globalize-symbol is specified for a given symbol, it will be
334     // global in the output file even if it is not included via
335     // --keep-global-symbol. Because of that, make sure to check
336     // --globalize-symbol second.
337     if (!Config.SymbolsToKeepGlobal.empty() &&
338         !Config.SymbolsToKeepGlobal.matches(Sym.Name) &&
339         Sym.getShndx() != SHN_UNDEF)
340       Sym.Binding = STB_LOCAL;
341 
342     if (Config.SymbolsToGlobalize.matches(Sym.Name) &&
343         Sym.getShndx() != SHN_UNDEF)
344       Sym.Binding = STB_GLOBAL;
345 
346     // SymbolsToWeaken applies to both STB_GLOBAL and STB_GNU_UNIQUE.
347     if (Config.SymbolsToWeaken.matches(Sym.Name) && Sym.Binding != STB_LOCAL)
348       Sym.Binding = STB_WEAK;
349 
350     if (Config.Weaken && Sym.Binding != STB_LOCAL &&
351         Sym.getShndx() != SHN_UNDEF)
352       Sym.Binding = STB_WEAK;
353 
354     const auto I = Config.SymbolsToRename.find(Sym.Name);
355     if (I != Config.SymbolsToRename.end())
356       Sym.Name = std::string(I->getValue());
357 
358     if (!Config.SymbolsPrefixRemove.empty() && Sym.Type != STT_SECTION)
359       if (Sym.Name.compare(0, Config.SymbolsPrefixRemove.size(),
360                            Config.SymbolsPrefixRemove) == 0)
361         Sym.Name = Sym.Name.substr(Config.SymbolsPrefixRemove.size());
362 
363     if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION)
364       Sym.Name = (Config.SymbolsPrefix + Sym.Name).str();
365   });
366 
367   // The purpose of this loop is to mark symbols referenced by sections
368   // (like GroupSection or RelocationSection). This way, we know which
369   // symbols are still 'needed' and which are not.
370   if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty() ||
371       !Config.OnlySection.empty()) {
372     for (SectionBase &Sec : Obj.sections())
373       Sec.markSymbols();
374   }
375 
376   auto RemoveSymbolsPred = [&](const Symbol &Sym) {
377     if (Config.SymbolsToKeep.matches(Sym.Name) ||
378         (ELFConfig.KeepFileSymbols && Sym.Type == STT_FILE))
379       return false;
380 
381     if (Config.SymbolsToRemove.matches(Sym.Name))
382       return true;
383 
384     if (Config.StripAll || Config.StripAllGNU)
385       return true;
386 
387     if (isRequiredByABISymbol(Obj, Sym))
388       return false;
389 
390     if (Config.StripDebug && Sym.Type == STT_FILE)
391       return true;
392 
393     if ((Config.DiscardMode == DiscardType::All ||
394          (Config.DiscardMode == DiscardType::Locals &&
395           StringRef(Sym.Name).starts_with(".L"))) &&
396         Sym.Binding == STB_LOCAL && Sym.getShndx() != SHN_UNDEF &&
397         Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
398       return true;
399 
400     if ((Config.StripUnneeded ||
401          Config.UnneededSymbolsToRemove.matches(Sym.Name)) &&
402         (!Obj.isRelocatable() || isUnneededSymbol(Sym)))
403       return true;
404 
405     // We want to remove undefined symbols if all references have been stripped.
406     if (!Config.OnlySection.empty() && !Sym.Referenced &&
407         Sym.getShndx() == SHN_UNDEF)
408       return true;
409 
410     return false;
411   };
412 
413   return Obj.removeSymbols(RemoveSymbolsPred);
414 }
415 
416 static Error replaceAndRemoveSections(const CommonConfig &Config,
417                                       const ELFConfig &ELFConfig, Object &Obj) {
418   SectionPred RemovePred = [](const SectionBase &) { return false; };
419 
420   // Removes:
421   if (!Config.ToRemove.empty()) {
422     RemovePred = [&Config](const SectionBase &Sec) {
423       return Config.ToRemove.matches(Sec.Name);
424     };
425   }
426 
427   if (Config.StripDWO)
428     RemovePred = [RemovePred](const SectionBase &Sec) {
429       return isDWOSection(Sec) || RemovePred(Sec);
430     };
431 
432   if (Config.ExtractDWO)
433     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
434       return onlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
435     };
436 
437   if (Config.StripAllGNU)
438     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
439       if (RemovePred(Sec))
440         return true;
441       if ((Sec.Flags & SHF_ALLOC) != 0)
442         return false;
443       if (&Sec == Obj.SectionNames)
444         return false;
445       switch (Sec.Type) {
446       case SHT_SYMTAB:
447       case SHT_REL:
448       case SHT_RELA:
449       case SHT_STRTAB:
450         return true;
451       }
452       return isDebugSection(Sec);
453     };
454 
455   if (Config.StripSections) {
456     RemovePred = [RemovePred](const SectionBase &Sec) {
457       return RemovePred(Sec) || Sec.ParentSegment == nullptr;
458     };
459   }
460 
461   if (Config.StripDebug || Config.StripUnneeded) {
462     RemovePred = [RemovePred](const SectionBase &Sec) {
463       return RemovePred(Sec) || isDebugSection(Sec);
464     };
465   }
466 
467   if (Config.StripNonAlloc)
468     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
469       if (RemovePred(Sec))
470         return true;
471       if (&Sec == Obj.SectionNames)
472         return false;
473       return (Sec.Flags & SHF_ALLOC) == 0 && Sec.ParentSegment == nullptr;
474     };
475 
476   if (Config.StripAll)
477     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
478       if (RemovePred(Sec))
479         return true;
480       if (&Sec == Obj.SectionNames)
481         return false;
482       if (StringRef(Sec.Name).starts_with(".gnu.warning"))
483         return false;
484       if (StringRef(Sec.Name).starts_with(".gnu_debuglink"))
485         return false;
486       // We keep the .ARM.attribute section to maintain compatibility
487       // with Debian derived distributions. This is a bug in their
488       // patchset as documented here:
489       // https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=943798
490       if (Sec.Type == SHT_ARM_ATTRIBUTES)
491         return false;
492       if (Sec.ParentSegment != nullptr)
493         return false;
494       return (Sec.Flags & SHF_ALLOC) == 0;
495     };
496 
497   if (Config.ExtractPartition || Config.ExtractMainPartition) {
498     RemovePred = [RemovePred](const SectionBase &Sec) {
499       if (RemovePred(Sec))
500         return true;
501       if (Sec.Type == SHT_LLVM_PART_EHDR || Sec.Type == SHT_LLVM_PART_PHDR)
502         return true;
503       return (Sec.Flags & SHF_ALLOC) != 0 && !Sec.ParentSegment;
504     };
505   }
506 
507   // Explicit copies:
508   if (!Config.OnlySection.empty()) {
509     RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
510       // Explicitly keep these sections regardless of previous removes.
511       if (Config.OnlySection.matches(Sec.Name))
512         return false;
513 
514       // Allow all implicit removes.
515       if (RemovePred(Sec))
516         return true;
517 
518       // Keep special sections.
519       if (Obj.SectionNames == &Sec)
520         return false;
521       if (Obj.SymbolTable == &Sec ||
522           (Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec))
523         return false;
524 
525       // Remove everything else.
526       return true;
527     };
528   }
529 
530   if (!Config.KeepSection.empty()) {
531     RemovePred = [&Config, RemovePred](const SectionBase &Sec) {
532       // Explicitly keep these sections regardless of previous removes.
533       if (Config.KeepSection.matches(Sec.Name))
534         return false;
535       // Otherwise defer to RemovePred.
536       return RemovePred(Sec);
537     };
538   }
539 
540   // This has to be the last predicate assignment.
541   // If the option --keep-symbol has been specified
542   // and at least one of those symbols is present
543   // (equivalently, the updated symbol table is not empty)
544   // the symbol table and the string table should not be removed.
545   if ((!Config.SymbolsToKeep.empty() || ELFConfig.KeepFileSymbols) &&
546       Obj.SymbolTable && !Obj.SymbolTable->empty()) {
547     RemovePred = [&Obj, RemovePred](const SectionBase &Sec) {
548       if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab())
549         return false;
550       return RemovePred(Sec);
551     };
552   }
553 
554   if (Error E = Obj.removeSections(ELFConfig.AllowBrokenLinks, RemovePred))
555     return E;
556 
557   if (Error E = Obj.compressOrDecompressSections(Config))
558     return E;
559 
560   return Error::success();
561 }
562 
563 // Add symbol to the Object symbol table with the specified properties.
564 static void addSymbol(Object &Obj, const NewSymbolInfo &SymInfo,
565                       uint8_t DefaultVisibility) {
566   SectionBase *Sec = Obj.findSection(SymInfo.SectionName);
567   uint64_t Value = Sec ? Sec->Addr + SymInfo.Value : SymInfo.Value;
568 
569   uint8_t Bind = ELF::STB_GLOBAL;
570   uint8_t Type = ELF::STT_NOTYPE;
571   uint8_t Visibility = DefaultVisibility;
572 
573   for (SymbolFlag FlagValue : SymInfo.Flags)
574     switch (FlagValue) {
575     case SymbolFlag::Global:
576       Bind = ELF::STB_GLOBAL;
577       break;
578     case SymbolFlag::Local:
579       Bind = ELF::STB_LOCAL;
580       break;
581     case SymbolFlag::Weak:
582       Bind = ELF::STB_WEAK;
583       break;
584     case SymbolFlag::Default:
585       Visibility = ELF::STV_DEFAULT;
586       break;
587     case SymbolFlag::Hidden:
588       Visibility = ELF::STV_HIDDEN;
589       break;
590     case SymbolFlag::Protected:
591       Visibility = ELF::STV_PROTECTED;
592       break;
593     case SymbolFlag::File:
594       Type = ELF::STT_FILE;
595       break;
596     case SymbolFlag::Section:
597       Type = ELF::STT_SECTION;
598       break;
599     case SymbolFlag::Object:
600       Type = ELF::STT_OBJECT;
601       break;
602     case SymbolFlag::Function:
603       Type = ELF::STT_FUNC;
604       break;
605     case SymbolFlag::IndirectFunction:
606       Type = ELF::STT_GNU_IFUNC;
607       break;
608     default: /* Other flag values are ignored for ELF. */
609       break;
610     };
611 
612   Obj.SymbolTable->addSymbol(
613       SymInfo.SymbolName, Bind, Type, Sec, Value, Visibility,
614       Sec ? (uint16_t)SYMBOL_SIMPLE_INDEX : (uint16_t)SHN_ABS, 0);
615 }
616 
617 static Error
618 handleUserSection(const NewSectionInfo &NewSection,
619                   function_ref<Error(StringRef, ArrayRef<uint8_t>)> F) {
620   ArrayRef<uint8_t> Data(reinterpret_cast<const uint8_t *>(
621                              NewSection.SectionData->getBufferStart()),
622                          NewSection.SectionData->getBufferSize());
623   return F(NewSection.SectionName, Data);
624 }
625 
626 static Error verifyNoteSection(StringRef Name, endianness Endianness,
627                                ArrayRef<uint8_t> Data) {
628   // An ELF note has the following structure:
629   // Name Size: 4 bytes (integer)
630   // Desc Size: 4 bytes (integer)
631   // Type     : 4 bytes
632   // Name     : variable size, padded to a 4 byte boundary
633   // Desc     : variable size, padded to a 4 byte boundary
634 
635   if (Data.empty())
636     return Error::success();
637 
638   if (Data.size() < 12) {
639     std::string msg;
640     raw_string_ostream(msg)
641         << Name << " data must be either empty or at least 12 bytes long";
642     return createStringError(errc::invalid_argument, msg);
643   }
644   if (Data.size() % 4 != 0) {
645     std::string msg;
646     raw_string_ostream(msg)
647         << Name << " data size must be a  multiple of 4 bytes";
648     return createStringError(errc::invalid_argument, msg);
649   }
650   ArrayRef<uint8_t> NameSize = Data.slice(0, 4);
651   ArrayRef<uint8_t> DescSize = Data.slice(4, 4);
652 
653   uint32_t NameSizeValue = support::endian::read32(NameSize.data(), Endianness);
654   uint32_t DescSizeValue = support::endian::read32(DescSize.data(), Endianness);
655 
656   uint64_t ExpectedDataSize =
657       /*NameSize=*/4 + /*DescSize=*/4 + /*Type=*/4 +
658       /*Name=*/alignTo(NameSizeValue, 4) +
659       /*Desc=*/alignTo(DescSizeValue, 4);
660   uint64_t ActualDataSize = Data.size();
661   if (ActualDataSize != ExpectedDataSize) {
662     std::string msg;
663     raw_string_ostream(msg)
664         << Name
665         << " data size is incompatible with the content of "
666            "the name and description size fields:"
667         << " expecting " << ExpectedDataSize << ", found " << ActualDataSize;
668     return createStringError(errc::invalid_argument, msg);
669   }
670 
671   return Error::success();
672 }
673 
674 // This function handles the high level operations of GNU objcopy including
675 // handling command line options. It's important to outline certain properties
676 // we expect to hold of the command line operations. Any operation that "keeps"
677 // should keep regardless of a remove. Additionally any removal should respect
678 // any previous removals. Lastly whether or not something is removed shouldn't
679 // depend a) on the order the options occur in or b) on some opaque priority
680 // system. The only priority is that keeps/copies overrule removes.
681 static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
682                         ElfType OutputElfType, Object &Obj) {
683   if (Config.OutputArch) {
684     Obj.Machine = Config.OutputArch->EMachine;
685     Obj.OSABI = Config.OutputArch->OSABI;
686   }
687 
688   if (!Config.SplitDWO.empty() && Config.ExtractDWO) {
689     return Obj.removeSections(
690         ELFConfig.AllowBrokenLinks,
691         [&Obj](const SectionBase &Sec) { return onlyKeepDWOPred(Obj, Sec); });
692   }
693 
694   // Dump sections before add/remove for compatibility with GNU objcopy.
695   for (StringRef Flag : Config.DumpSection) {
696     StringRef SectionName;
697     StringRef FileName;
698     std::tie(SectionName, FileName) = Flag.split('=');
699     if (Error E = dumpSectionToFile(SectionName, FileName, Obj))
700       return E;
701   }
702 
703   // It is important to remove the sections first. For example, we want to
704   // remove the relocation sections before removing the symbols. That allows
705   // us to avoid reporting the inappropriate errors about removing symbols
706   // named in relocations.
707   if (Error E = replaceAndRemoveSections(Config, ELFConfig, Obj))
708     return E;
709 
710   if (Error E = updateAndRemoveSymbols(Config, ELFConfig, Obj))
711     return E;
712 
713   if (!Config.SetSectionAlignment.empty()) {
714     for (SectionBase &Sec : Obj.sections()) {
715       auto I = Config.SetSectionAlignment.find(Sec.Name);
716       if (I != Config.SetSectionAlignment.end())
717         Sec.Align = I->second;
718     }
719   }
720 
721   if (Config.ChangeSectionLMAValAll != 0) {
722     for (Segment &Seg : Obj.segments()) {
723       if (Seg.FileSize > 0) {
724         if (Config.ChangeSectionLMAValAll > 0 &&
725             Seg.PAddr > std::numeric_limits<uint64_t>::max() -
726                             Config.ChangeSectionLMAValAll) {
727           return createStringError(
728               errc::invalid_argument,
729               "address 0x" + Twine::utohexstr(Seg.PAddr) +
730                   " cannot be increased by 0x" +
731                   Twine::utohexstr(Config.ChangeSectionLMAValAll) +
732                   ". The result would overflow");
733         } else if (Config.ChangeSectionLMAValAll < 0 &&
734                    Seg.PAddr < std::numeric_limits<uint64_t>::min() -
735                                    Config.ChangeSectionLMAValAll) {
736           return createStringError(
737               errc::invalid_argument,
738               "address 0x" + Twine::utohexstr(Seg.PAddr) +
739                   " cannot be decreased by 0x" +
740                   Twine::utohexstr(std::abs(Config.ChangeSectionLMAValAll)) +
741                   ". The result would underflow");
742         }
743         Seg.PAddr += Config.ChangeSectionLMAValAll;
744       }
745     }
746   }
747 
748   if (Config.OnlyKeepDebug)
749     for (auto &Sec : Obj.sections())
750       if (Sec.Flags & SHF_ALLOC && Sec.Type != SHT_NOTE)
751         Sec.Type = SHT_NOBITS;
752 
753   endianness E = OutputElfType == ELFT_ELF32LE || OutputElfType == ELFT_ELF64LE
754                      ? endianness::little
755                      : endianness::big;
756 
757   for (const NewSectionInfo &AddedSection : Config.AddSection) {
758     auto AddSection = [&](StringRef Name, ArrayRef<uint8_t> Data) -> Error {
759       OwnedDataSection &NewSection =
760           Obj.addSection<OwnedDataSection>(Name, Data);
761       if (Name.starts_with(".note") && Name != ".note.GNU-stack") {
762         NewSection.Type = SHT_NOTE;
763         if (ELFConfig.VerifyNoteSections)
764           return verifyNoteSection(Name, E, Data);
765       }
766       return Error::success();
767     };
768     if (Error E = handleUserSection(AddedSection, AddSection))
769       return E;
770   }
771 
772   for (const NewSectionInfo &NewSection : Config.UpdateSection) {
773     auto UpdateSection = [&](StringRef Name, ArrayRef<uint8_t> Data) {
774       return Obj.updateSection(Name, Data);
775     };
776     if (Error E = handleUserSection(NewSection, UpdateSection))
777       return E;
778   }
779 
780   if (!Config.AddGnuDebugLink.empty())
781     Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink,
782                                         Config.GnuDebugLinkCRC32);
783 
784   // If the symbol table was previously removed, we need to create a new one
785   // before adding new symbols.
786   if (!Obj.SymbolTable && !Config.SymbolsToAdd.empty())
787     if (Error E = Obj.addNewSymbolTable())
788       return E;
789 
790   for (const NewSymbolInfo &SI : Config.SymbolsToAdd)
791     addSymbol(Obj, SI, ELFConfig.NewSymbolVisibility);
792 
793   // --set-section-{flags,type} work with sections added by --add-section.
794   if (!Config.SetSectionFlags.empty() || !Config.SetSectionType.empty()) {
795     for (auto &Sec : Obj.sections()) {
796       const auto Iter = Config.SetSectionFlags.find(Sec.Name);
797       if (Iter != Config.SetSectionFlags.end()) {
798         const SectionFlagsUpdate &SFU = Iter->second;
799         if (Error E = setSectionFlagsAndType(Sec, SFU.NewFlags, Obj.Machine))
800           return E;
801       }
802       auto It2 = Config.SetSectionType.find(Sec.Name);
803       if (It2 != Config.SetSectionType.end())
804         setSectionType(Sec, It2->second);
805     }
806   }
807 
808   if (!Config.SectionsToRename.empty()) {
809     std::vector<RelocationSectionBase *> RelocSections;
810     DenseSet<SectionBase *> RenamedSections;
811     for (SectionBase &Sec : Obj.sections()) {
812       auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec);
813       const auto Iter = Config.SectionsToRename.find(Sec.Name);
814       if (Iter != Config.SectionsToRename.end()) {
815         const SectionRename &SR = Iter->second;
816         Sec.Name = std::string(SR.NewName);
817         if (SR.NewFlags) {
818           if (Error E = setSectionFlagsAndType(Sec, *SR.NewFlags, Obj.Machine))
819             return E;
820         }
821         RenamedSections.insert(&Sec);
822       } else if (RelocSec && !(Sec.Flags & SHF_ALLOC))
823         // Postpone processing relocation sections which are not specified in
824         // their explicit '--rename-section' commands until after their target
825         // sections are renamed.
826         // Dynamic relocation sections (i.e. ones with SHF_ALLOC) should be
827         // renamed only explicitly. Otherwise, renaming, for example, '.got.plt'
828         // would affect '.rela.plt', which is not desirable.
829         RelocSections.push_back(RelocSec);
830     }
831 
832     // Rename relocation sections according to their target sections.
833     for (RelocationSectionBase *RelocSec : RelocSections) {
834       auto Iter = RenamedSections.find(RelocSec->getSection());
835       if (Iter != RenamedSections.end())
836         RelocSec->Name = (RelocSec->getNamePrefix() + (*Iter)->Name).str();
837     }
838   }
839 
840   // Add a prefix to allocated sections and their relocation sections. This
841   // should be done after renaming the section by Config.SectionToRename to
842   // imitate the GNU objcopy behavior.
843   if (!Config.AllocSectionsPrefix.empty()) {
844     DenseSet<SectionBase *> PrefixedSections;
845     for (SectionBase &Sec : Obj.sections()) {
846       if (Sec.Flags & SHF_ALLOC) {
847         Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str();
848         PrefixedSections.insert(&Sec);
849       } else if (auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) {
850         // Rename relocation sections associated to the allocated sections.
851         // For example, if we rename .text to .prefix.text, we also rename
852         // .rel.text to .rel.prefix.text.
853         //
854         // Dynamic relocation sections (SHT_REL[A] with SHF_ALLOC) are handled
855         // above, e.g., .rela.plt is renamed to .prefix.rela.plt, not
856         // .rela.prefix.plt since GNU objcopy does so.
857         const SectionBase *TargetSec = RelocSec->getSection();
858         if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) {
859           // If the relocation section comes *after* the target section, we
860           // don't add Config.AllocSectionsPrefix because we've already added
861           // the prefix to TargetSec->Name. Otherwise, if the relocation
862           // section comes *before* the target section, we add the prefix.
863           if (PrefixedSections.count(TargetSec))
864             Sec.Name = (RelocSec->getNamePrefix() + TargetSec->Name).str();
865           else
866             Sec.Name = (RelocSec->getNamePrefix() + Config.AllocSectionsPrefix +
867                         TargetSec->Name)
868                            .str();
869         }
870       }
871     }
872   }
873 
874   if (ELFConfig.EntryExpr)
875     Obj.Entry = ELFConfig.EntryExpr(Obj.Entry);
876   return Error::success();
877 }
878 
879 static Error writeOutput(const CommonConfig &Config, Object &Obj,
880                          raw_ostream &Out, ElfType OutputElfType) {
881   std::unique_ptr<Writer> Writer =
882       createWriter(Config, Obj, Out, OutputElfType);
883   if (Error E = Writer->finalize())
884     return E;
885   return Writer->write();
886 }
887 
888 Error objcopy::elf::executeObjcopyOnIHex(const CommonConfig &Config,
889                                          const ELFConfig &ELFConfig,
890                                          MemoryBuffer &In, raw_ostream &Out) {
891   IHexReader Reader(&In);
892   Expected<std::unique_ptr<Object>> Obj = Reader.create(true);
893   if (!Obj)
894     return Obj.takeError();
895 
896   const ElfType OutputElfType =
897       getOutputElfType(Config.OutputArch.value_or(MachineInfo()));
898   if (Error E = handleArgs(Config, ELFConfig, OutputElfType, **Obj))
899     return E;
900   return writeOutput(Config, **Obj, Out, OutputElfType);
901 }
902 
903 Error objcopy::elf::executeObjcopyOnRawBinary(const CommonConfig &Config,
904                                               const ELFConfig &ELFConfig,
905                                               MemoryBuffer &In,
906                                               raw_ostream &Out) {
907   BinaryReader Reader(&In, ELFConfig.NewSymbolVisibility);
908   Expected<std::unique_ptr<Object>> Obj = Reader.create(true);
909   if (!Obj)
910     return Obj.takeError();
911 
912   // Prefer OutputArch (-O<format>) if set, otherwise fallback to BinaryArch
913   // (-B<arch>).
914   const ElfType OutputElfType =
915       getOutputElfType(Config.OutputArch.value_or(MachineInfo()));
916   if (Error E = handleArgs(Config, ELFConfig, OutputElfType, **Obj))
917     return E;
918   return writeOutput(Config, **Obj, Out, OutputElfType);
919 }
920 
921 Error objcopy::elf::executeObjcopyOnBinary(const CommonConfig &Config,
922                                            const ELFConfig &ELFConfig,
923                                            object::ELFObjectFileBase &In,
924                                            raw_ostream &Out) {
925   ELFReader Reader(&In, Config.ExtractPartition);
926   Expected<std::unique_ptr<Object>> Obj =
927       Reader.create(!Config.SymbolsToAdd.empty());
928   if (!Obj)
929     return Obj.takeError();
930   // Prefer OutputArch (-O<format>) if set, otherwise infer it from the input.
931   const ElfType OutputElfType = Config.OutputArch
932                                     ? getOutputElfType(*Config.OutputArch)
933                                     : getOutputElfType(In);
934 
935   if (Error E = handleArgs(Config, ELFConfig, OutputElfType, **Obj))
936     return createFileError(Config.InputFilename, std::move(E));
937 
938   if (Error E = writeOutput(Config, **Obj, Out, OutputElfType))
939     return createFileError(Config.InputFilename, std::move(E));
940 
941   return Error::success();
942 }
943