Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 1 | //===- ELFObjcopy.cpp -----------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "ELFObjcopy.h" |
| 10 | #include "Buffer.h" |
| 11 | #include "CopyConfig.h" |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 12 | #include "Object.h" |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 13 | #include "llvm-objcopy.h" |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 14 | |
| 15 | #include "llvm/ADT/BitmaskEnum.h" |
| 16 | #include "llvm/ADT/Optional.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
| 20 | #include "llvm/ADT/Twine.h" |
| 21 | #include "llvm/BinaryFormat/ELF.h" |
| 22 | #include "llvm/MC/MCTargetOptions.h" |
| 23 | #include "llvm/Object/Binary.h" |
| 24 | #include "llvm/Object/ELFObjectFile.h" |
| 25 | #include "llvm/Object/ELFTypes.h" |
| 26 | #include "llvm/Object/Error.h" |
| 27 | #include "llvm/Option/Option.h" |
| 28 | #include "llvm/Support/Casting.h" |
| 29 | #include "llvm/Support/Compression.h" |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Errc.h" |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Error.h" |
| 32 | #include "llvm/Support/ErrorHandling.h" |
| 33 | #include "llvm/Support/ErrorOr.h" |
| 34 | #include "llvm/Support/Memory.h" |
| 35 | #include "llvm/Support/Path.h" |
| 36 | #include "llvm/Support/raw_ostream.h" |
| 37 | #include <algorithm> |
| 38 | #include <cassert> |
| 39 | #include <cstdlib> |
| 40 | #include <functional> |
| 41 | #include <iterator> |
| 42 | #include <memory> |
| 43 | #include <string> |
| 44 | #include <system_error> |
| 45 | #include <utility> |
| 46 | |
| 47 | namespace llvm { |
| 48 | namespace objcopy { |
| 49 | namespace elf { |
| 50 | |
| 51 | using namespace object; |
| 52 | using namespace ELF; |
| 53 | using SectionPred = std::function<bool(const SectionBase &Sec)>; |
| 54 | |
| 55 | static bool isDebugSection(const SectionBase &Sec) { |
| 56 | return StringRef(Sec.Name).startswith(".debug") || |
| 57 | StringRef(Sec.Name).startswith(".zdebug") || Sec.Name == ".gdb_index"; |
| 58 | } |
| 59 | |
| 60 | static bool isDWOSection(const SectionBase &Sec) { |
| 61 | return StringRef(Sec.Name).endswith(".dwo"); |
| 62 | } |
| 63 | |
| 64 | static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) { |
| 65 | // We can't remove the section header string table. |
| 66 | if (&Sec == Obj.SectionNames) |
| 67 | return false; |
| 68 | // Short of keeping the string table we want to keep everything that is a DWO |
| 69 | // section and remove everything else. |
| 70 | return !isDWOSection(Sec); |
| 71 | } |
| 72 | |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 73 | static uint64_t setSectionFlagsPreserveMask(uint64_t OldFlags, |
| 74 | uint64_t NewFlags) { |
| 75 | // Preserve some flags which should not be dropped when setting flags. |
| 76 | // Also, preserve anything OS/processor dependant. |
| 77 | const uint64_t PreserveMask = ELF::SHF_COMPRESSED | ELF::SHF_EXCLUDE | |
| 78 | ELF::SHF_GROUP | ELF::SHF_LINK_ORDER | |
| 79 | ELF::SHF_MASKOS | ELF::SHF_MASKPROC | |
| 80 | ELF::SHF_TLS | ELF::SHF_INFO_LINK; |
| 81 | return (OldFlags & PreserveMask) | (NewFlags & ~PreserveMask); |
| 82 | } |
| 83 | |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 84 | static ElfType getOutputElfType(const Binary &Bin) { |
| 85 | // Infer output ELF type from the input ELF object |
| 86 | if (isa<ELFObjectFile<ELF32LE>>(Bin)) |
| 87 | return ELFT_ELF32LE; |
| 88 | if (isa<ELFObjectFile<ELF64LE>>(Bin)) |
| 89 | return ELFT_ELF64LE; |
| 90 | if (isa<ELFObjectFile<ELF32BE>>(Bin)) |
| 91 | return ELFT_ELF32BE; |
| 92 | if (isa<ELFObjectFile<ELF64BE>>(Bin)) |
| 93 | return ELFT_ELF64BE; |
| 94 | llvm_unreachable("Invalid ELFType"); |
| 95 | } |
| 96 | |
| 97 | static ElfType getOutputElfType(const MachineInfo &MI) { |
| 98 | // Infer output ELF type from the binary arch specified |
| 99 | if (MI.Is64Bit) |
| 100 | return MI.IsLittleEndian ? ELFT_ELF64LE : ELFT_ELF64BE; |
| 101 | else |
| 102 | return MI.IsLittleEndian ? ELFT_ELF32LE : ELFT_ELF32BE; |
| 103 | } |
| 104 | |
| 105 | static std::unique_ptr<Writer> createWriter(const CopyConfig &Config, |
| 106 | Object &Obj, Buffer &Buf, |
| 107 | ElfType OutputElfType) { |
| 108 | if (Config.OutputFormat == "binary") { |
| 109 | return llvm::make_unique<BinaryWriter>(Obj, Buf); |
| 110 | } |
| 111 | // Depending on the initial ELFT and OutputFormat we need a different Writer. |
| 112 | switch (OutputElfType) { |
| 113 | case ELFT_ELF32LE: |
| 114 | return llvm::make_unique<ELFWriter<ELF32LE>>(Obj, Buf, |
| 115 | !Config.StripSections); |
| 116 | case ELFT_ELF64LE: |
| 117 | return llvm::make_unique<ELFWriter<ELF64LE>>(Obj, Buf, |
| 118 | !Config.StripSections); |
| 119 | case ELFT_ELF32BE: |
| 120 | return llvm::make_unique<ELFWriter<ELF32BE>>(Obj, Buf, |
| 121 | !Config.StripSections); |
| 122 | case ELFT_ELF64BE: |
| 123 | return llvm::make_unique<ELFWriter<ELF64BE>>(Obj, Buf, |
| 124 | !Config.StripSections); |
| 125 | } |
| 126 | llvm_unreachable("Invalid output format"); |
| 127 | } |
| 128 | |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 129 | template <class ELFT> |
| 130 | static Expected<ArrayRef<uint8_t>> |
| 131 | findBuildID(const object::ELFFile<ELFT> &In) { |
| 132 | for (const auto &Phdr : unwrapOrError(In.program_headers())) { |
| 133 | if (Phdr.p_type != PT_NOTE) |
| 134 | continue; |
| 135 | Error Err = Error::success(); |
David Blaikie | ba005aa | 2018-12-11 00:09:06 +0000 | [diff] [blame] | 136 | for (const auto &Note : In.notes(Phdr, Err)) |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 137 | if (Note.getType() == NT_GNU_BUILD_ID && Note.getName() == ELF_NOTE_GNU) |
| 138 | return Note.getDesc(); |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 139 | if (Err) |
| 140 | return std::move(Err); |
| 141 | } |
| 142 | return createStringError(llvm::errc::invalid_argument, |
| 143 | "Could not find build ID."); |
| 144 | } |
| 145 | |
| 146 | static Expected<ArrayRef<uint8_t>> |
| 147 | findBuildID(const object::ELFObjectFileBase &In) { |
| 148 | if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(&In)) |
| 149 | return findBuildID(*O->getELFFile()); |
| 150 | else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(&In)) |
| 151 | return findBuildID(*O->getELFFile()); |
| 152 | else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(&In)) |
| 153 | return findBuildID(*O->getELFFile()); |
| 154 | else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(&In)) |
| 155 | return findBuildID(*O->getELFFile()); |
| 156 | |
| 157 | llvm_unreachable("Bad file format"); |
| 158 | } |
| 159 | |
Jordan Rupprecht | fc832e9 | 2019-01-30 18:13:30 +0000 | [diff] [blame] | 160 | static Error linkToBuildIdDir(const CopyConfig &Config, StringRef ToLink, |
| 161 | StringRef Suffix, |
| 162 | ArrayRef<uint8_t> BuildIdBytes) { |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 163 | SmallString<128> Path = Config.BuildIdLinkDir; |
| 164 | sys::path::append(Path, llvm::toHex(BuildIdBytes[0], /*LowerCase*/ true)); |
| 165 | if (auto EC = sys::fs::create_directories(Path)) |
Jordan Rupprecht | fc832e9 | 2019-01-30 18:13:30 +0000 | [diff] [blame] | 166 | return createFileError( |
| 167 | Path.str(), |
| 168 | createStringError(EC, "cannot create build ID link directory")); |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 169 | |
| 170 | sys::path::append(Path, |
| 171 | llvm::toHex(BuildIdBytes.slice(1), /*LowerCase*/ true)); |
| 172 | Path += Suffix; |
| 173 | if (auto EC = sys::fs::create_hard_link(ToLink, Path)) { |
| 174 | // Hard linking failed, try to remove the file first if it exists. |
| 175 | if (sys::fs::exists(Path)) |
| 176 | sys::fs::remove(Path); |
| 177 | EC = sys::fs::create_hard_link(ToLink, Path); |
| 178 | if (EC) |
Jordan Rupprecht | fc832e9 | 2019-01-30 18:13:30 +0000 | [diff] [blame] | 179 | return createStringError(EC, "cannot link %s to %s", ToLink.data(), |
| 180 | Path.data()); |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 181 | } |
Jordan Rupprecht | fc832e9 | 2019-01-30 18:13:30 +0000 | [diff] [blame] | 182 | return Error::success(); |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 185 | static Error splitDWOToFile(const CopyConfig &Config, const Reader &Reader, |
| 186 | StringRef File, ElfType OutputElfType) { |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 187 | auto DWOFile = Reader.create(); |
Jordan Rupprecht | 971d4762 | 2019-02-01 15:20:36 +0000 | [diff] [blame] | 188 | auto OnlyKeepDWOPred = [&DWOFile](const SectionBase &Sec) { |
| 189 | return onlyKeepDWOPred(*DWOFile, Sec); |
| 190 | }; |
| 191 | if (Error E = DWOFile->removeSections(OnlyKeepDWOPred)) |
| 192 | return E; |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 193 | if (Config.OutputArch) |
| 194 | DWOFile->Machine = Config.OutputArch.getValue().EMachine; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 195 | FileBuffer FB(File); |
| 196 | auto Writer = createWriter(Config, *DWOFile, FB, OutputElfType); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 197 | if (Error E = Writer->finalize()) |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 198 | return E; |
| 199 | return Writer->write(); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static Error dumpSectionToFile(StringRef SecName, StringRef Filename, |
| 203 | Object &Obj) { |
| 204 | for (auto &Sec : Obj.sections()) { |
| 205 | if (Sec.Name == SecName) { |
Jordan Rupprecht | 16a0de2 | 2018-12-20 00:57:06 +0000 | [diff] [blame] | 206 | if (Sec.OriginalData.empty()) |
Martin Storsjo | 8010c6be | 2019-01-22 10:57:59 +0000 | [diff] [blame] | 207 | return createStringError( |
| 208 | object_error::parse_failed, |
| 209 | "Can't dump section \"%s\": it has no contents", |
| 210 | SecName.str().c_str()); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 211 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 212 | FileOutputBuffer::create(Filename, Sec.OriginalData.size()); |
| 213 | if (!BufferOrErr) |
| 214 | return BufferOrErr.takeError(); |
| 215 | std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr); |
| 216 | std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), |
| 217 | Buf->getBufferStart()); |
| 218 | if (Error E = Buf->commit()) |
| 219 | return E; |
| 220 | return Error::success(); |
| 221 | } |
| 222 | } |
Martin Storsjo | 8010c6be | 2019-01-22 10:57:59 +0000 | [diff] [blame] | 223 | return createStringError(object_error::parse_failed, "Section not found"); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 226 | static bool isCompressable(const SectionBase &Section) { |
George Rimar | ade3c70 | 2019-03-05 13:07:43 +0000 | [diff] [blame] | 227 | return !(Section.Flags & ELF::SHF_COMPRESSED) && |
| 228 | StringRef(Section.Name).startswith(".debug"); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static void replaceDebugSections( |
Fangrui Song | 3dfc3fb | 2019-03-15 10:27:28 +0000 | [diff] [blame^] | 232 | Object &Obj, SectionPred &RemovePred, |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 233 | function_ref<bool(const SectionBase &)> shouldReplace, |
| 234 | function_ref<SectionBase *(const SectionBase *)> addSection) { |
George Rimar | d8a5c6c | 2019-03-11 11:01:24 +0000 | [diff] [blame] | 235 | // Build a list of the debug sections we are going to replace. |
| 236 | // We can't call `addSection` while iterating over sections, |
| 237 | // because it would mutate the sections array. |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 238 | SmallVector<SectionBase *, 13> ToReplace; |
George Rimar | d8a5c6c | 2019-03-11 11:01:24 +0000 | [diff] [blame] | 239 | for (auto &Sec : Obj.sections()) |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 240 | if (shouldReplace(Sec)) |
| 241 | ToReplace.push_back(&Sec); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 242 | |
George Rimar | d8a5c6c | 2019-03-11 11:01:24 +0000 | [diff] [blame] | 243 | // Build a mapping from original section to a new one. |
| 244 | DenseMap<SectionBase *, SectionBase *> FromTo; |
| 245 | for (SectionBase *S : ToReplace) |
| 246 | FromTo[S] = addSection(S); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 247 | |
George Rimar | d8a5c6c | 2019-03-11 11:01:24 +0000 | [diff] [blame] | 248 | // Now we want to update the target sections of relocation |
| 249 | // sections. Also we will update the relocations themselves |
| 250 | // to update the symbol references. |
| 251 | for (auto &Sec : Obj.sections()) |
| 252 | Sec.replaceSectionReferences(FromTo); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 253 | |
| 254 | RemovePred = [shouldReplace, RemovePred](const SectionBase &Sec) { |
| 255 | return shouldReplace(Sec) || RemovePred(Sec); |
| 256 | }; |
| 257 | } |
| 258 | |
Eugene Leviant | 2db1062 | 2019-02-13 07:34:54 +0000 | [diff] [blame] | 259 | static bool isUnneededSymbol(const Symbol &Sym) { |
| 260 | return !Sym.Referenced && |
| 261 | (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) && |
| 262 | Sym.Type != STT_FILE && Sym.Type != STT_SECTION; |
| 263 | } |
| 264 | |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 265 | // This function handles the high level operations of GNU objcopy including |
| 266 | // handling command line options. It's important to outline certain properties |
| 267 | // we expect to hold of the command line operations. Any operation that "keeps" |
| 268 | // should keep regardless of a remove. Additionally any removal should respect |
| 269 | // any previous removals. Lastly whether or not something is removed shouldn't |
| 270 | // depend a) on the order the options occur in or b) on some opaque priority |
| 271 | // system. The only priority is that keeps/copies overrule removes. |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 272 | static Error handleArgs(const CopyConfig &Config, Object &Obj, |
| 273 | const Reader &Reader, ElfType OutputElfType) { |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 274 | |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 275 | if (!Config.SplitDWO.empty()) |
| 276 | if (Error E = |
| 277 | splitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType)) |
| 278 | return E; |
| 279 | |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 280 | if (Config.OutputArch) |
| 281 | Obj.Machine = Config.OutputArch.getValue().EMachine; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 282 | |
| 283 | // TODO: update or remove symbols only if there is an option that affects |
| 284 | // them. |
| 285 | if (Obj.SymbolTable) { |
| 286 | Obj.SymbolTable->updateSymbols([&](Symbol &Sym) { |
Jordan Rupprecht | bd7735f | 2019-01-31 16:45:16 +0000 | [diff] [blame] | 287 | // Common and undefined symbols don't make sense as local symbols, and can |
| 288 | // even cause crashes if we localize those, so skip them. |
| 289 | if (!Sym.isCommon() && Sym.getShndx() != SHN_UNDEF && |
Jordan Rupprecht | b47475c | 2018-11-01 17:26:36 +0000 | [diff] [blame] | 290 | ((Config.LocalizeHidden && |
| 291 | (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) || |
Fangrui Song | e4ee066 | 2018-11-29 17:32:51 +0000 | [diff] [blame] | 292 | is_contained(Config.SymbolsToLocalize, Sym.Name))) |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 293 | Sym.Binding = STB_LOCAL; |
| 294 | |
| 295 | // Note: these two globalize flags have very similar names but different |
| 296 | // meanings: |
| 297 | // |
| 298 | // --globalize-symbol: promote a symbol to global |
| 299 | // --keep-global-symbol: all symbols except for these should be made local |
| 300 | // |
| 301 | // If --globalize-symbol is specified for a given symbol, it will be |
| 302 | // global in the output file even if it is not included via |
| 303 | // --keep-global-symbol. Because of that, make sure to check |
| 304 | // --globalize-symbol second. |
| 305 | if (!Config.SymbolsToKeepGlobal.empty() && |
Jordan Rupprecht | 634820d | 2018-10-30 16:23:38 +0000 | [diff] [blame] | 306 | !is_contained(Config.SymbolsToKeepGlobal, Sym.Name) && |
| 307 | Sym.getShndx() != SHN_UNDEF) |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 308 | Sym.Binding = STB_LOCAL; |
| 309 | |
Fangrui Song | e4ee066 | 2018-11-29 17:32:51 +0000 | [diff] [blame] | 310 | if (is_contained(Config.SymbolsToGlobalize, Sym.Name) && |
Jordan Rupprecht | 634820d | 2018-10-30 16:23:38 +0000 | [diff] [blame] | 311 | Sym.getShndx() != SHN_UNDEF) |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 312 | Sym.Binding = STB_GLOBAL; |
| 313 | |
Fangrui Song | e4ee066 | 2018-11-29 17:32:51 +0000 | [diff] [blame] | 314 | if (is_contained(Config.SymbolsToWeaken, Sym.Name) && |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 315 | Sym.Binding == STB_GLOBAL) |
| 316 | Sym.Binding = STB_WEAK; |
| 317 | |
| 318 | if (Config.Weaken && Sym.Binding == STB_GLOBAL && |
| 319 | Sym.getShndx() != SHN_UNDEF) |
| 320 | Sym.Binding = STB_WEAK; |
| 321 | |
| 322 | const auto I = Config.SymbolsToRename.find(Sym.Name); |
| 323 | if (I != Config.SymbolsToRename.end()) |
| 324 | Sym.Name = I->getValue(); |
| 325 | |
| 326 | if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION) |
| 327 | Sym.Name = (Config.SymbolsPrefix + Sym.Name).str(); |
| 328 | }); |
| 329 | |
| 330 | // The purpose of this loop is to mark symbols referenced by sections |
| 331 | // (like GroupSection or RelocationSection). This way, we know which |
| 332 | // symbols are still 'needed' and which are not. |
Eugene Leviant | 2db1062 | 2019-02-13 07:34:54 +0000 | [diff] [blame] | 333 | if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty()) { |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 334 | for (auto &Section : Obj.sections()) |
| 335 | Section.markSymbols(); |
| 336 | } |
| 337 | |
Jordan Rupprecht | 971d4762 | 2019-02-01 15:20:36 +0000 | [diff] [blame] | 338 | auto RemoveSymbolsPred = [&](const Symbol &Sym) { |
Fangrui Song | e4ee066 | 2018-11-29 17:32:51 +0000 | [diff] [blame] | 339 | if (is_contained(Config.SymbolsToKeep, Sym.Name) || |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 340 | (Config.KeepFileSymbols && Sym.Type == STT_FILE)) |
| 341 | return false; |
| 342 | |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 343 | if ((Config.DiscardMode == DiscardType::All || |
| 344 | (Config.DiscardMode == DiscardType::Locals && |
| 345 | StringRef(Sym.Name).startswith(".L"))) && |
| 346 | Sym.Binding == STB_LOCAL && Sym.getShndx() != SHN_UNDEF && |
| 347 | Sym.Type != STT_FILE && Sym.Type != STT_SECTION) |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 348 | return true; |
| 349 | |
| 350 | if (Config.StripAll || Config.StripAllGNU) |
| 351 | return true; |
| 352 | |
Fangrui Song | e4ee066 | 2018-11-29 17:32:51 +0000 | [diff] [blame] | 353 | if (is_contained(Config.SymbolsToRemove, Sym.Name)) |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 354 | return true; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 355 | |
Eugene Leviant | 2db1062 | 2019-02-13 07:34:54 +0000 | [diff] [blame] | 356 | if ((Config.StripUnneeded || |
| 357 | is_contained(Config.UnneededSymbolsToRemove, Sym.Name)) && |
| 358 | isUnneededSymbol(Sym)) |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 359 | return true; |
| 360 | |
| 361 | return false; |
Jordan Rupprecht | 971d4762 | 2019-02-01 15:20:36 +0000 | [diff] [blame] | 362 | }; |
| 363 | if (Error E = Obj.removeSymbols(RemoveSymbolsPred)) |
| 364 | return E; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | SectionPred RemovePred = [](const SectionBase &) { return false; }; |
| 368 | |
| 369 | // Removes: |
| 370 | if (!Config.ToRemove.empty()) { |
| 371 | RemovePred = [&Config](const SectionBase &Sec) { |
| 372 | return is_contained(Config.ToRemove, Sec.Name); |
| 373 | }; |
| 374 | } |
| 375 | |
| 376 | if (Config.StripDWO || !Config.SplitDWO.empty()) |
| 377 | RemovePred = [RemovePred](const SectionBase &Sec) { |
| 378 | return isDWOSection(Sec) || RemovePred(Sec); |
| 379 | }; |
| 380 | |
| 381 | if (Config.ExtractDWO) |
| 382 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 383 | return onlyKeepDWOPred(Obj, Sec) || RemovePred(Sec); |
| 384 | }; |
| 385 | |
| 386 | if (Config.StripAllGNU) |
| 387 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 388 | if (RemovePred(Sec)) |
| 389 | return true; |
| 390 | if ((Sec.Flags & SHF_ALLOC) != 0) |
| 391 | return false; |
| 392 | if (&Sec == Obj.SectionNames) |
| 393 | return false; |
| 394 | switch (Sec.Type) { |
| 395 | case SHT_SYMTAB: |
| 396 | case SHT_REL: |
| 397 | case SHT_RELA: |
| 398 | case SHT_STRTAB: |
| 399 | return true; |
| 400 | } |
| 401 | return isDebugSection(Sec); |
| 402 | }; |
| 403 | |
| 404 | if (Config.StripSections) { |
| 405 | RemovePred = [RemovePred](const SectionBase &Sec) { |
James Henderson | b5de5e2 | 2019-03-14 11:47:41 +0000 | [diff] [blame] | 406 | return RemovePred(Sec) || Sec.ParentSegment == nullptr; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 407 | }; |
| 408 | } |
| 409 | |
| 410 | if (Config.StripDebug) { |
| 411 | RemovePred = [RemovePred](const SectionBase &Sec) { |
| 412 | return RemovePred(Sec) || isDebugSection(Sec); |
| 413 | }; |
| 414 | } |
| 415 | |
| 416 | if (Config.StripNonAlloc) |
| 417 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 418 | if (RemovePred(Sec)) |
| 419 | return true; |
| 420 | if (&Sec == Obj.SectionNames) |
| 421 | return false; |
James Henderson | b5de5e2 | 2019-03-14 11:47:41 +0000 | [diff] [blame] | 422 | return (Sec.Flags & SHF_ALLOC) == 0 && Sec.ParentSegment == nullptr; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 423 | }; |
| 424 | |
| 425 | if (Config.StripAll) |
| 426 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 427 | if (RemovePred(Sec)) |
| 428 | return true; |
| 429 | if (&Sec == Obj.SectionNames) |
| 430 | return false; |
| 431 | if (StringRef(Sec.Name).startswith(".gnu.warning")) |
| 432 | return false; |
James Henderson | b5de5e2 | 2019-03-14 11:47:41 +0000 | [diff] [blame] | 433 | if (Sec.ParentSegment != nullptr) |
| 434 | return false; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 435 | return (Sec.Flags & SHF_ALLOC) == 0; |
| 436 | }; |
| 437 | |
| 438 | // Explicit copies: |
Jake Ehrlich | 85985ed | 2018-12-06 02:03:53 +0000 | [diff] [blame] | 439 | if (!Config.OnlySection.empty()) { |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 440 | RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) { |
| 441 | // Explicitly keep these sections regardless of previous removes. |
Jake Ehrlich | 85985ed | 2018-12-06 02:03:53 +0000 | [diff] [blame] | 442 | if (is_contained(Config.OnlySection, Sec.Name)) |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 443 | return false; |
| 444 | |
| 445 | // Allow all implicit removes. |
| 446 | if (RemovePred(Sec)) |
| 447 | return true; |
| 448 | |
| 449 | // Keep special sections. |
| 450 | if (Obj.SectionNames == &Sec) |
| 451 | return false; |
| 452 | if (Obj.SymbolTable == &Sec || |
| 453 | (Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec)) |
| 454 | return false; |
| 455 | |
| 456 | // Remove everything else. |
| 457 | return true; |
| 458 | }; |
| 459 | } |
| 460 | |
Jordan Rupprecht | c5bae78 | 2018-11-13 19:32:27 +0000 | [diff] [blame] | 461 | if (!Config.KeepSection.empty()) { |
Fangrui Song | e9f34b0 | 2018-11-12 23:46:22 +0000 | [diff] [blame] | 462 | RemovePred = [&Config, RemovePred](const SectionBase &Sec) { |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 463 | // Explicitly keep these sections regardless of previous removes. |
Jordan Rupprecht | c5bae78 | 2018-11-13 19:32:27 +0000 | [diff] [blame] | 464 | if (is_contained(Config.KeepSection, Sec.Name)) |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 465 | return false; |
| 466 | // Otherwise defer to RemovePred. |
| 467 | return RemovePred(Sec); |
| 468 | }; |
| 469 | } |
| 470 | |
| 471 | // This has to be the last predicate assignment. |
| 472 | // If the option --keep-symbol has been specified |
| 473 | // and at least one of those symbols is present |
| 474 | // (equivalently, the updated symbol table is not empty) |
| 475 | // the symbol table and the string table should not be removed. |
| 476 | if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) && |
| 477 | Obj.SymbolTable && !Obj.SymbolTable->empty()) { |
| 478 | RemovePred = [&Obj, RemovePred](const SectionBase &Sec) { |
| 479 | if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab()) |
| 480 | return false; |
| 481 | return RemovePred(Sec); |
| 482 | }; |
| 483 | } |
| 484 | |
| 485 | if (Config.CompressionType != DebugCompressionType::None) |
Fangrui Song | 3dfc3fb | 2019-03-15 10:27:28 +0000 | [diff] [blame^] | 486 | replaceDebugSections(Obj, RemovePred, isCompressable, |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 487 | [&Config, &Obj](const SectionBase *S) { |
| 488 | return &Obj.addSection<CompressedSection>( |
| 489 | *S, Config.CompressionType); |
| 490 | }); |
| 491 | else if (Config.DecompressDebugSections) |
| 492 | replaceDebugSections( |
Fangrui Song | 3dfc3fb | 2019-03-15 10:27:28 +0000 | [diff] [blame^] | 493 | Obj, RemovePred, |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 494 | [](const SectionBase &S) { return isa<CompressedSection>(&S); }, |
| 495 | [&Obj](const SectionBase *S) { |
| 496 | auto CS = cast<CompressedSection>(S); |
| 497 | return &Obj.addSection<DecompressedSection>(*CS); |
| 498 | }); |
| 499 | |
Jordan Rupprecht | 971d4762 | 2019-02-01 15:20:36 +0000 | [diff] [blame] | 500 | if (Error E = Obj.removeSections(RemovePred)) |
| 501 | return E; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 502 | |
| 503 | if (!Config.SectionsToRename.empty()) { |
| 504 | for (auto &Sec : Obj.sections()) { |
| 505 | const auto Iter = Config.SectionsToRename.find(Sec.Name); |
| 506 | if (Iter != Config.SectionsToRename.end()) { |
| 507 | const SectionRename &SR = Iter->second; |
| 508 | Sec.Name = SR.NewName; |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 509 | if (SR.NewFlags.hasValue()) |
| 510 | Sec.Flags = |
| 511 | setSectionFlagsPreserveMask(Sec.Flags, SR.NewFlags.getValue()); |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | if (!Config.SetSectionFlags.empty()) { |
| 517 | for (auto &Sec : Obj.sections()) { |
| 518 | const auto Iter = Config.SetSectionFlags.find(Sec.Name); |
| 519 | if (Iter != Config.SetSectionFlags.end()) { |
| 520 | const SectionFlagsUpdate &SFU = Iter->second; |
| 521 | Sec.Flags = setSectionFlagsPreserveMask(Sec.Flags, SFU.NewFlags); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | } |
Eugene Leviant | c76671b | 2019-03-12 12:41:06 +0000 | [diff] [blame] | 525 | |
| 526 | for (const auto &Flag : Config.AddSection) { |
| 527 | std::pair<StringRef, StringRef> SecPair = Flag.split("="); |
| 528 | StringRef SecName = SecPair.first; |
| 529 | StringRef File = SecPair.second; |
| 530 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = |
| 531 | MemoryBuffer::getFile(File); |
| 532 | if (!BufOrErr) |
| 533 | return createFileError(File, errorCodeToError(BufOrErr.getError())); |
| 534 | std::unique_ptr<MemoryBuffer> Buf = std::move(*BufOrErr); |
| 535 | ArrayRef<uint8_t> Data( |
| 536 | reinterpret_cast<const uint8_t *>(Buf->getBufferStart()), |
| 537 | Buf->getBufferSize()); |
| 538 | OwnedDataSection &NewSection = |
| 539 | Obj.addSection<OwnedDataSection>(SecName, Data); |
| 540 | if (SecName.startswith(".note") && SecName != ".note.GNU-stack") |
| 541 | NewSection.Type = SHT_NOTE; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Eugene Leviant | c76671b | 2019-03-12 12:41:06 +0000 | [diff] [blame] | 544 | for (const auto &Flag : Config.DumpSection) { |
| 545 | std::pair<StringRef, StringRef> SecPair = Flag.split("="); |
| 546 | StringRef SecName = SecPair.first; |
| 547 | StringRef File = SecPair.second; |
| 548 | if (Error E = dumpSectionToFile(SecName, File, Obj)) |
| 549 | return createFileError(Config.InputFilename, std::move(E)); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | if (!Config.AddGnuDebugLink.empty()) |
| 553 | Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink); |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 554 | |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 555 | for (const NewSymbolInfo &SI : Config.SymbolsToAdd) { |
| 556 | SectionBase *Sec = Obj.findSection(SI.SectionName); |
| 557 | uint64_t Value = Sec ? Sec->Addr + SI.Value : SI.Value; |
Simon Pilgrim | 65706cf | 2019-02-27 10:19:53 +0000 | [diff] [blame] | 558 | Obj.SymbolTable->addSymbol( |
| 559 | SI.SymbolName, SI.Bind, SI.Type, Sec, Value, SI.Visibility, |
| 560 | Sec ? (uint16_t)SYMBOL_SIMPLE_INDEX : (uint16_t)SHN_ABS, 0); |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Eugene Leviant | 53350d0 | 2019-02-26 09:24:22 +0000 | [diff] [blame] | 563 | if (Config.EntryExpr) |
| 564 | Obj.Entry = Config.EntryExpr(Obj.Entry); |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 565 | return Error::success(); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 568 | Error executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In, |
| 569 | Buffer &Out) { |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 570 | BinaryReader Reader(Config.BinaryArch, &In); |
| 571 | std::unique_ptr<Object> Obj = Reader.create(); |
| 572 | |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 573 | // Prefer OutputArch (-O<format>) if set, otherwise fallback to BinaryArch |
| 574 | // (-B<arch>). |
| 575 | const ElfType OutputElfType = getOutputElfType( |
| 576 | Config.OutputArch ? Config.OutputArch.getValue() : Config.BinaryArch); |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 577 | if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType)) |
| 578 | return E; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 579 | std::unique_ptr<Writer> Writer = |
| 580 | createWriter(Config, *Obj, Out, OutputElfType); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 581 | if (Error E = Writer->finalize()) |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 582 | return E; |
| 583 | return Writer->write(); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 586 | Error executeObjcopyOnBinary(const CopyConfig &Config, |
| 587 | object::ELFObjectFileBase &In, Buffer &Out) { |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 588 | ELFReader Reader(&In); |
| 589 | std::unique_ptr<Object> Obj = Reader.create(); |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 590 | // Prefer OutputArch (-O<format>) if set, otherwise infer it from the input. |
| 591 | const ElfType OutputElfType = |
| 592 | Config.OutputArch ? getOutputElfType(Config.OutputArch.getValue()) |
| 593 | : getOutputElfType(In); |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 594 | ArrayRef<uint8_t> BuildIdBytes; |
| 595 | |
| 596 | if (!Config.BuildIdLinkDir.empty()) { |
| 597 | BuildIdBytes = unwrapOrError(findBuildID(In)); |
| 598 | if (BuildIdBytes.size() < 2) |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 599 | return createFileError( |
| 600 | Config.InputFilename, |
| 601 | createStringError(object_error::parse_failed, |
| 602 | "build ID is smaller than two bytes.")); |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Jordan Rupprecht | fc832e9 | 2019-01-30 18:13:30 +0000 | [diff] [blame] | 605 | if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkInput) |
| 606 | if (Error E = |
| 607 | linkToBuildIdDir(Config, Config.InputFilename, |
| 608 | Config.BuildIdLinkInput.getValue(), BuildIdBytes)) |
| 609 | return E; |
| 610 | |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 611 | if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType)) |
| 612 | return E; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 613 | std::unique_ptr<Writer> Writer = |
| 614 | createWriter(Config, *Obj, Out, OutputElfType); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 615 | if (Error E = Writer->finalize()) |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 616 | return E; |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 617 | if (Error E = Writer->write()) |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 618 | return E; |
Jordan Rupprecht | fc832e9 | 2019-01-30 18:13:30 +0000 | [diff] [blame] | 619 | if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkOutput) |
| 620 | if (Error E = |
| 621 | linkToBuildIdDir(Config, Config.OutputFilename, |
| 622 | Config.BuildIdLinkOutput.getValue(), BuildIdBytes)) |
| 623 | return E; |
| 624 | |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 625 | return Error::success(); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | } // end namespace elf |
| 629 | } // end namespace objcopy |
| 630 | } // end namespace llvm |