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" |
James Henderson | fa11fb3 | 2019-05-08 09:49:35 +0000 | [diff] [blame^] | 16 | #include "llvm/ADT/DenseSet.h" |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Optional.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include "llvm/ADT/Twine.h" |
| 22 | #include "llvm/BinaryFormat/ELF.h" |
| 23 | #include "llvm/MC/MCTargetOptions.h" |
| 24 | #include "llvm/Object/Binary.h" |
| 25 | #include "llvm/Object/ELFObjectFile.h" |
| 26 | #include "llvm/Object/ELFTypes.h" |
| 27 | #include "llvm/Object/Error.h" |
| 28 | #include "llvm/Option/Option.h" |
| 29 | #include "llvm/Support/Casting.h" |
| 30 | #include "llvm/Support/Compression.h" |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Errc.h" |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Error.h" |
| 33 | #include "llvm/Support/ErrorHandling.h" |
| 34 | #include "llvm/Support/ErrorOr.h" |
| 35 | #include "llvm/Support/Memory.h" |
| 36 | #include "llvm/Support/Path.h" |
| 37 | #include "llvm/Support/raw_ostream.h" |
| 38 | #include <algorithm> |
| 39 | #include <cassert> |
| 40 | #include <cstdlib> |
| 41 | #include <functional> |
| 42 | #include <iterator> |
| 43 | #include <memory> |
| 44 | #include <string> |
| 45 | #include <system_error> |
| 46 | #include <utility> |
| 47 | |
| 48 | namespace llvm { |
| 49 | namespace objcopy { |
| 50 | namespace elf { |
| 51 | |
| 52 | using namespace object; |
| 53 | using namespace ELF; |
| 54 | using SectionPred = std::function<bool(const SectionBase &Sec)>; |
| 55 | |
| 56 | static bool isDebugSection(const SectionBase &Sec) { |
| 57 | return StringRef(Sec.Name).startswith(".debug") || |
| 58 | StringRef(Sec.Name).startswith(".zdebug") || Sec.Name == ".gdb_index"; |
| 59 | } |
| 60 | |
| 61 | static bool isDWOSection(const SectionBase &Sec) { |
| 62 | return StringRef(Sec.Name).endswith(".dwo"); |
| 63 | } |
| 64 | |
| 65 | static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) { |
| 66 | // We can't remove the section header string table. |
| 67 | if (&Sec == Obj.SectionNames) |
| 68 | return false; |
| 69 | // Short of keeping the string table we want to keep everything that is a DWO |
| 70 | // section and remove everything else. |
| 71 | return !isDWOSection(Sec); |
| 72 | } |
| 73 | |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 74 | uint64_t getNewShfFlags(SectionFlag AllFlags) { |
| 75 | uint64_t NewFlags = 0; |
| 76 | if (AllFlags & SectionFlag::SecAlloc) |
| 77 | NewFlags |= ELF::SHF_ALLOC; |
| 78 | if (!(AllFlags & SectionFlag::SecReadonly)) |
| 79 | NewFlags |= ELF::SHF_WRITE; |
| 80 | if (AllFlags & SectionFlag::SecCode) |
| 81 | NewFlags |= ELF::SHF_EXECINSTR; |
| 82 | if (AllFlags & SectionFlag::SecMerge) |
| 83 | NewFlags |= ELF::SHF_MERGE; |
| 84 | if (AllFlags & SectionFlag::SecStrings) |
| 85 | NewFlags |= ELF::SHF_STRINGS; |
| 86 | return NewFlags; |
| 87 | } |
| 88 | |
Jordan Rupprecht | 017deaf | 2019-04-02 16:49:56 +0000 | [diff] [blame] | 89 | static uint64_t getSectionFlagsPreserveMask(uint64_t OldFlags, |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 90 | uint64_t NewFlags) { |
| 91 | // Preserve some flags which should not be dropped when setting flags. |
| 92 | // Also, preserve anything OS/processor dependant. |
| 93 | const uint64_t PreserveMask = ELF::SHF_COMPRESSED | ELF::SHF_EXCLUDE | |
| 94 | ELF::SHF_GROUP | ELF::SHF_LINK_ORDER | |
| 95 | ELF::SHF_MASKOS | ELF::SHF_MASKPROC | |
| 96 | ELF::SHF_TLS | ELF::SHF_INFO_LINK; |
| 97 | return (OldFlags & PreserveMask) | (NewFlags & ~PreserveMask); |
| 98 | } |
| 99 | |
Jordan Rupprecht | 017deaf | 2019-04-02 16:49:56 +0000 | [diff] [blame] | 100 | static void setSectionFlagsAndType(SectionBase &Sec, SectionFlag Flags) { |
| 101 | Sec.Flags = getSectionFlagsPreserveMask(Sec.Flags, getNewShfFlags(Flags)); |
| 102 | |
Fangrui Song | aa1f2c5 | 2019-05-01 00:39:31 +0000 | [diff] [blame] | 103 | // In GNU objcopy, certain flags promote SHT_NOBITS to SHT_PROGBITS. This rule |
| 104 | // may promote more non-ALLOC sections than GNU objcopy, but it is fine as |
| 105 | // non-ALLOC SHT_NOBITS sections do not make much sense. |
| 106 | if (Sec.Type == SHT_NOBITS && |
| 107 | (!(Sec.Flags & ELF::SHF_ALLOC) || |
| 108 | Flags & (SectionFlag::SecContents | SectionFlag::SecLoad))) |
Jordan Rupprecht | 017deaf | 2019-04-02 16:49:56 +0000 | [diff] [blame] | 109 | Sec.Type = SHT_PROGBITS; |
| 110 | } |
| 111 | |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 112 | static ElfType getOutputElfType(const Binary &Bin) { |
| 113 | // Infer output ELF type from the input ELF object |
| 114 | if (isa<ELFObjectFile<ELF32LE>>(Bin)) |
| 115 | return ELFT_ELF32LE; |
| 116 | if (isa<ELFObjectFile<ELF64LE>>(Bin)) |
| 117 | return ELFT_ELF64LE; |
| 118 | if (isa<ELFObjectFile<ELF32BE>>(Bin)) |
| 119 | return ELFT_ELF32BE; |
| 120 | if (isa<ELFObjectFile<ELF64BE>>(Bin)) |
| 121 | return ELFT_ELF64BE; |
| 122 | llvm_unreachable("Invalid ELFType"); |
| 123 | } |
| 124 | |
| 125 | static ElfType getOutputElfType(const MachineInfo &MI) { |
| 126 | // Infer output ELF type from the binary arch specified |
| 127 | if (MI.Is64Bit) |
| 128 | return MI.IsLittleEndian ? ELFT_ELF64LE : ELFT_ELF64BE; |
| 129 | else |
| 130 | return MI.IsLittleEndian ? ELFT_ELF32LE : ELFT_ELF32BE; |
| 131 | } |
| 132 | |
| 133 | static std::unique_ptr<Writer> createWriter(const CopyConfig &Config, |
| 134 | Object &Obj, Buffer &Buf, |
| 135 | ElfType OutputElfType) { |
| 136 | if (Config.OutputFormat == "binary") { |
| 137 | return llvm::make_unique<BinaryWriter>(Obj, Buf); |
| 138 | } |
| 139 | // Depending on the initial ELFT and OutputFormat we need a different Writer. |
| 140 | switch (OutputElfType) { |
| 141 | case ELFT_ELF32LE: |
| 142 | return llvm::make_unique<ELFWriter<ELF32LE>>(Obj, Buf, |
| 143 | !Config.StripSections); |
| 144 | case ELFT_ELF64LE: |
| 145 | return llvm::make_unique<ELFWriter<ELF64LE>>(Obj, Buf, |
| 146 | !Config.StripSections); |
| 147 | case ELFT_ELF32BE: |
| 148 | return llvm::make_unique<ELFWriter<ELF32BE>>(Obj, Buf, |
| 149 | !Config.StripSections); |
| 150 | case ELFT_ELF64BE: |
| 151 | return llvm::make_unique<ELFWriter<ELF64BE>>(Obj, Buf, |
| 152 | !Config.StripSections); |
| 153 | } |
| 154 | llvm_unreachable("Invalid output format"); |
| 155 | } |
| 156 | |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 157 | template <class ELFT> |
| 158 | static Expected<ArrayRef<uint8_t>> |
| 159 | findBuildID(const object::ELFFile<ELFT> &In) { |
| 160 | for (const auto &Phdr : unwrapOrError(In.program_headers())) { |
| 161 | if (Phdr.p_type != PT_NOTE) |
| 162 | continue; |
| 163 | Error Err = Error::success(); |
David Blaikie | ba005aa | 2018-12-11 00:09:06 +0000 | [diff] [blame] | 164 | for (const auto &Note : In.notes(Phdr, Err)) |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 165 | if (Note.getType() == NT_GNU_BUILD_ID && Note.getName() == ELF_NOTE_GNU) |
| 166 | return Note.getDesc(); |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 167 | if (Err) |
| 168 | return std::move(Err); |
| 169 | } |
| 170 | return createStringError(llvm::errc::invalid_argument, |
| 171 | "Could not find build ID."); |
| 172 | } |
| 173 | |
| 174 | static Expected<ArrayRef<uint8_t>> |
| 175 | findBuildID(const object::ELFObjectFileBase &In) { |
| 176 | if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(&In)) |
| 177 | return findBuildID(*O->getELFFile()); |
| 178 | else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(&In)) |
| 179 | return findBuildID(*O->getELFFile()); |
| 180 | else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(&In)) |
| 181 | return findBuildID(*O->getELFFile()); |
| 182 | else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(&In)) |
| 183 | return findBuildID(*O->getELFFile()); |
| 184 | |
| 185 | llvm_unreachable("Bad file format"); |
| 186 | } |
| 187 | |
Jake Ehrlich | 5049c34 | 2019-03-18 20:35:18 +0000 | [diff] [blame] | 188 | template <class... Ts> |
| 189 | static Error makeStringError(std::error_code EC, const Twine &Msg, Ts&&... Args) { |
| 190 | std::string FullMsg = (EC.message() + ": " + Msg).str(); |
| 191 | return createStringError(EC, FullMsg.c_str(), std::forward<Ts>(Args)...); |
| 192 | } |
| 193 | |
| 194 | #define MODEL_8 "%%%%%%%%" |
| 195 | #define MODEL_16 MODEL_8 MODEL_8 |
| 196 | #define MODEL_32 (MODEL_16 MODEL_16) |
| 197 | |
Jordan Rupprecht | fc832e9 | 2019-01-30 18:13:30 +0000 | [diff] [blame] | 198 | static Error linkToBuildIdDir(const CopyConfig &Config, StringRef ToLink, |
| 199 | StringRef Suffix, |
| 200 | ArrayRef<uint8_t> BuildIdBytes) { |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 201 | SmallString<128> Path = Config.BuildIdLinkDir; |
| 202 | sys::path::append(Path, llvm::toHex(BuildIdBytes[0], /*LowerCase*/ true)); |
| 203 | if (auto EC = sys::fs::create_directories(Path)) |
Jordan Rupprecht | fc832e9 | 2019-01-30 18:13:30 +0000 | [diff] [blame] | 204 | return createFileError( |
| 205 | Path.str(), |
Jake Ehrlich | 5049c34 | 2019-03-18 20:35:18 +0000 | [diff] [blame] | 206 | makeStringError(EC, "cannot create build ID link directory")); |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 207 | |
| 208 | sys::path::append(Path, |
| 209 | llvm::toHex(BuildIdBytes.slice(1), /*LowerCase*/ true)); |
| 210 | Path += Suffix; |
Jake Ehrlich | 5049c34 | 2019-03-18 20:35:18 +0000 | [diff] [blame] | 211 | SmallString<128> TmpPath; |
| 212 | // create_hard_link races so we need to link to a temporary path but |
| 213 | // we want to make sure that we choose a filename that does not exist. |
| 214 | // By using 32 model characters we get 128-bits of entropy. It is |
| 215 | // unlikely that this string has ever existed before much less exists |
| 216 | // on this disk or in the current working directory. |
| 217 | // Additionally we prepend the original Path for debugging but also |
| 218 | // because it ensures that we're linking within a directory on the same |
| 219 | // partition on the same device which is critical. It has the added |
| 220 | // win of yet further decreasing the odds of a conflict. |
| 221 | sys::fs::createUniquePath(Twine(Path) + "-" + MODEL_32 + ".tmp", TmpPath, |
| 222 | /*MakeAbsolute*/ false); |
| 223 | if (auto EC = sys::fs::create_hard_link(ToLink, TmpPath)) { |
| 224 | Path.push_back('\0'); |
| 225 | return makeStringError(EC, "cannot link %s to %s", ToLink.data(), |
| 226 | Path.data()); |
| 227 | } |
| 228 | // We then atomically rename the link into place which will just move the |
| 229 | // link. If rename fails something is more seriously wrong so just return |
| 230 | // an error. |
| 231 | if (auto EC = sys::fs::rename(TmpPath, Path)) { |
| 232 | Path.push_back('\0'); |
| 233 | return makeStringError(EC, "cannot link %s to %s", ToLink.data(), |
| 234 | Path.data()); |
| 235 | } |
| 236 | // If `Path` was already a hard-link to the same underlying file then the |
| 237 | // temp file will be left so we need to remove it. Remove will not cause |
| 238 | // an error by default if the file is already gone so just blindly remove |
| 239 | // it rather than checking. |
| 240 | if (auto EC = sys::fs::remove(TmpPath)) { |
| 241 | TmpPath.push_back('\0'); |
| 242 | return makeStringError(EC, "could not remove %s", TmpPath.data()); |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 243 | } |
Jordan Rupprecht | fc832e9 | 2019-01-30 18:13:30 +0000 | [diff] [blame] | 244 | return Error::success(); |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 247 | static Error splitDWOToFile(const CopyConfig &Config, const Reader &Reader, |
| 248 | StringRef File, ElfType OutputElfType) { |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 249 | auto DWOFile = Reader.create(); |
Jordan Rupprecht | 971d4762 | 2019-02-01 15:20:36 +0000 | [diff] [blame] | 250 | auto OnlyKeepDWOPred = [&DWOFile](const SectionBase &Sec) { |
| 251 | return onlyKeepDWOPred(*DWOFile, Sec); |
| 252 | }; |
James Henderson | 66a9d0f | 2019-04-18 09:13:30 +0000 | [diff] [blame] | 253 | if (Error E = DWOFile->removeSections(Config.AllowBrokenLinks, |
| 254 | OnlyKeepDWOPred)) |
Jordan Rupprecht | 971d4762 | 2019-02-01 15:20:36 +0000 | [diff] [blame] | 255 | return E; |
James Henderson | c040d5d | 2019-03-22 10:21:09 +0000 | [diff] [blame] | 256 | if (Config.OutputArch) { |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 257 | DWOFile->Machine = Config.OutputArch.getValue().EMachine; |
James Henderson | c040d5d | 2019-03-22 10:21:09 +0000 | [diff] [blame] | 258 | DWOFile->OSABI = Config.OutputArch.getValue().OSABI; |
| 259 | } |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 260 | FileBuffer FB(File); |
| 261 | auto Writer = createWriter(Config, *DWOFile, FB, OutputElfType); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 262 | if (Error E = Writer->finalize()) |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 263 | return E; |
| 264 | return Writer->write(); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static Error dumpSectionToFile(StringRef SecName, StringRef Filename, |
| 268 | Object &Obj) { |
| 269 | for (auto &Sec : Obj.sections()) { |
| 270 | if (Sec.Name == SecName) { |
Jordan Rupprecht | 16a0de2 | 2018-12-20 00:57:06 +0000 | [diff] [blame] | 271 | if (Sec.OriginalData.empty()) |
Martin Storsjo | 8010c6be | 2019-01-22 10:57:59 +0000 | [diff] [blame] | 272 | return createStringError( |
| 273 | object_error::parse_failed, |
| 274 | "Can't dump section \"%s\": it has no contents", |
| 275 | SecName.str().c_str()); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 276 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 277 | FileOutputBuffer::create(Filename, Sec.OriginalData.size()); |
| 278 | if (!BufferOrErr) |
| 279 | return BufferOrErr.takeError(); |
| 280 | std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr); |
| 281 | std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), |
| 282 | Buf->getBufferStart()); |
| 283 | if (Error E = Buf->commit()) |
| 284 | return E; |
| 285 | return Error::success(); |
| 286 | } |
| 287 | } |
Martin Storsjo | 8010c6be | 2019-01-22 10:57:59 +0000 | [diff] [blame] | 288 | return createStringError(object_error::parse_failed, "Section not found"); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 291 | static bool isCompressable(const SectionBase &Section) { |
George Rimar | ade3c70 | 2019-03-05 13:07:43 +0000 | [diff] [blame] | 292 | return !(Section.Flags & ELF::SHF_COMPRESSED) && |
| 293 | StringRef(Section.Name).startswith(".debug"); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static void replaceDebugSections( |
Fangrui Song | 3dfc3fb | 2019-03-15 10:27:28 +0000 | [diff] [blame] | 297 | Object &Obj, SectionPred &RemovePred, |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 298 | function_ref<bool(const SectionBase &)> shouldReplace, |
| 299 | function_ref<SectionBase *(const SectionBase *)> addSection) { |
George Rimar | d8a5c6c | 2019-03-11 11:01:24 +0000 | [diff] [blame] | 300 | // Build a list of the debug sections we are going to replace. |
| 301 | // We can't call `addSection` while iterating over sections, |
| 302 | // because it would mutate the sections array. |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 303 | SmallVector<SectionBase *, 13> ToReplace; |
George Rimar | d8a5c6c | 2019-03-11 11:01:24 +0000 | [diff] [blame] | 304 | for (auto &Sec : Obj.sections()) |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 305 | if (shouldReplace(Sec)) |
| 306 | ToReplace.push_back(&Sec); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 307 | |
George Rimar | d8a5c6c | 2019-03-11 11:01:24 +0000 | [diff] [blame] | 308 | // Build a mapping from original section to a new one. |
| 309 | DenseMap<SectionBase *, SectionBase *> FromTo; |
| 310 | for (SectionBase *S : ToReplace) |
| 311 | FromTo[S] = addSection(S); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 312 | |
George Rimar | d8a5c6c | 2019-03-11 11:01:24 +0000 | [diff] [blame] | 313 | // Now we want to update the target sections of relocation |
| 314 | // sections. Also we will update the relocations themselves |
| 315 | // to update the symbol references. |
| 316 | for (auto &Sec : Obj.sections()) |
| 317 | Sec.replaceSectionReferences(FromTo); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 318 | |
| 319 | RemovePred = [shouldReplace, RemovePred](const SectionBase &Sec) { |
| 320 | return shouldReplace(Sec) || RemovePred(Sec); |
| 321 | }; |
| 322 | } |
| 323 | |
Eugene Leviant | 2db1062 | 2019-02-13 07:34:54 +0000 | [diff] [blame] | 324 | static bool isUnneededSymbol(const Symbol &Sym) { |
| 325 | return !Sym.Referenced && |
| 326 | (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) && |
| 327 | Sym.Type != STT_FILE && Sym.Type != STT_SECTION; |
| 328 | } |
| 329 | |
George Rimar | e6963be | 2019-03-25 12:34:25 +0000 | [diff] [blame] | 330 | static Error updateAndRemoveSymbols(const CopyConfig &Config, Object &Obj) { |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 331 | // TODO: update or remove symbols only if there is an option that affects |
| 332 | // them. |
George Rimar | e6963be | 2019-03-25 12:34:25 +0000 | [diff] [blame] | 333 | if (!Obj.SymbolTable) |
| 334 | return Error::success(); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 335 | |
George Rimar | e6963be | 2019-03-25 12:34:25 +0000 | [diff] [blame] | 336 | Obj.SymbolTable->updateSymbols([&](Symbol &Sym) { |
| 337 | // Common and undefined symbols don't make sense as local symbols, and can |
| 338 | // even cause crashes if we localize those, so skip them. |
| 339 | if (!Sym.isCommon() && Sym.getShndx() != SHN_UNDEF && |
| 340 | ((Config.LocalizeHidden && |
| 341 | (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) || |
| 342 | is_contained(Config.SymbolsToLocalize, Sym.Name))) |
| 343 | Sym.Binding = STB_LOCAL; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 344 | |
George Rimar | e6963be | 2019-03-25 12:34:25 +0000 | [diff] [blame] | 345 | // Note: these two globalize flags have very similar names but different |
| 346 | // meanings: |
| 347 | // |
| 348 | // --globalize-symbol: promote a symbol to global |
| 349 | // --keep-global-symbol: all symbols except for these should be made local |
| 350 | // |
| 351 | // If --globalize-symbol is specified for a given symbol, it will be |
| 352 | // global in the output file even if it is not included via |
| 353 | // --keep-global-symbol. Because of that, make sure to check |
| 354 | // --globalize-symbol second. |
| 355 | if (!Config.SymbolsToKeepGlobal.empty() && |
| 356 | !is_contained(Config.SymbolsToKeepGlobal, Sym.Name) && |
| 357 | Sym.getShndx() != SHN_UNDEF) |
| 358 | Sym.Binding = STB_LOCAL; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 359 | |
George Rimar | e6963be | 2019-03-25 12:34:25 +0000 | [diff] [blame] | 360 | if (is_contained(Config.SymbolsToGlobalize, Sym.Name) && |
| 361 | Sym.getShndx() != SHN_UNDEF) |
| 362 | Sym.Binding = STB_GLOBAL; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 363 | |
George Rimar | e6963be | 2019-03-25 12:34:25 +0000 | [diff] [blame] | 364 | if (is_contained(Config.SymbolsToWeaken, Sym.Name) && |
| 365 | Sym.Binding == STB_GLOBAL) |
| 366 | Sym.Binding = STB_WEAK; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 367 | |
George Rimar | e6963be | 2019-03-25 12:34:25 +0000 | [diff] [blame] | 368 | if (Config.Weaken && Sym.Binding == STB_GLOBAL && |
| 369 | Sym.getShndx() != SHN_UNDEF) |
| 370 | Sym.Binding = STB_WEAK; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 371 | |
George Rimar | e6963be | 2019-03-25 12:34:25 +0000 | [diff] [blame] | 372 | const auto I = Config.SymbolsToRename.find(Sym.Name); |
| 373 | if (I != Config.SymbolsToRename.end()) |
| 374 | Sym.Name = I->getValue(); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 375 | |
George Rimar | e6963be | 2019-03-25 12:34:25 +0000 | [diff] [blame] | 376 | if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION) |
| 377 | Sym.Name = (Config.SymbolsPrefix + Sym.Name).str(); |
| 378 | }); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 379 | |
George Rimar | e6963be | 2019-03-25 12:34:25 +0000 | [diff] [blame] | 380 | // The purpose of this loop is to mark symbols referenced by sections |
| 381 | // (like GroupSection or RelocationSection). This way, we know which |
| 382 | // symbols are still 'needed' and which are not. |
| 383 | if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty()) { |
| 384 | for (auto &Section : Obj.sections()) |
| 385 | Section.markSymbols(); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 386 | } |
| 387 | |
George Rimar | e6963be | 2019-03-25 12:34:25 +0000 | [diff] [blame] | 388 | auto RemoveSymbolsPred = [&](const Symbol &Sym) { |
| 389 | if (is_contained(Config.SymbolsToKeep, Sym.Name) || |
| 390 | (Config.KeepFileSymbols && Sym.Type == STT_FILE)) |
| 391 | return false; |
| 392 | |
| 393 | if ((Config.DiscardMode == DiscardType::All || |
| 394 | (Config.DiscardMode == DiscardType::Locals && |
| 395 | StringRef(Sym.Name).startswith(".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.StripAll || Config.StripAllGNU) |
| 401 | return true; |
| 402 | |
| 403 | if (is_contained(Config.SymbolsToRemove, Sym.Name)) |
| 404 | return true; |
| 405 | |
| 406 | if ((Config.StripUnneeded || |
| 407 | is_contained(Config.UnneededSymbolsToRemove, Sym.Name)) && |
| 408 | isUnneededSymbol(Sym)) |
| 409 | return true; |
| 410 | |
| 411 | return false; |
| 412 | }; |
| 413 | |
| 414 | return Obj.removeSymbols(RemoveSymbolsPred); |
| 415 | } |
| 416 | |
| 417 | static Error replaceAndRemoveSections(const CopyConfig &Config, Object &Obj) { |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 418 | SectionPred RemovePred = [](const SectionBase &) { return false; }; |
| 419 | |
| 420 | // Removes: |
| 421 | if (!Config.ToRemove.empty()) { |
| 422 | RemovePred = [&Config](const SectionBase &Sec) { |
| 423 | return is_contained(Config.ToRemove, Sec.Name); |
| 424 | }; |
| 425 | } |
| 426 | |
| 427 | if (Config.StripDWO || !Config.SplitDWO.empty()) |
| 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) { |
James Henderson | b5de5e2 | 2019-03-14 11:47:41 +0000 | [diff] [blame] | 457 | return RemovePred(Sec) || Sec.ParentSegment == nullptr; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 458 | }; |
| 459 | } |
| 460 | |
| 461 | if (Config.StripDebug) { |
| 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; |
James Henderson | b5de5e2 | 2019-03-14 11:47:41 +0000 | [diff] [blame] | 473 | return (Sec.Flags & SHF_ALLOC) == 0 && Sec.ParentSegment == nullptr; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 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).startswith(".gnu.warning")) |
| 483 | return false; |
James Henderson | b5de5e2 | 2019-03-14 11:47:41 +0000 | [diff] [blame] | 484 | if (Sec.ParentSegment != nullptr) |
| 485 | return false; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 486 | return (Sec.Flags & SHF_ALLOC) == 0; |
| 487 | }; |
| 488 | |
| 489 | // Explicit copies: |
Jake Ehrlich | 85985ed | 2018-12-06 02:03:53 +0000 | [diff] [blame] | 490 | if (!Config.OnlySection.empty()) { |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 491 | RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) { |
| 492 | // Explicitly keep these sections regardless of previous removes. |
Jake Ehrlich | 85985ed | 2018-12-06 02:03:53 +0000 | [diff] [blame] | 493 | if (is_contained(Config.OnlySection, Sec.Name)) |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 494 | return false; |
| 495 | |
| 496 | // Allow all implicit removes. |
| 497 | if (RemovePred(Sec)) |
| 498 | return true; |
| 499 | |
| 500 | // Keep special sections. |
| 501 | if (Obj.SectionNames == &Sec) |
| 502 | return false; |
| 503 | if (Obj.SymbolTable == &Sec || |
| 504 | (Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec)) |
| 505 | return false; |
| 506 | |
| 507 | // Remove everything else. |
| 508 | return true; |
| 509 | }; |
| 510 | } |
| 511 | |
Jordan Rupprecht | c5bae78 | 2018-11-13 19:32:27 +0000 | [diff] [blame] | 512 | if (!Config.KeepSection.empty()) { |
Fangrui Song | e9f34b0 | 2018-11-12 23:46:22 +0000 | [diff] [blame] | 513 | RemovePred = [&Config, RemovePred](const SectionBase &Sec) { |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 514 | // Explicitly keep these sections regardless of previous removes. |
Jordan Rupprecht | c5bae78 | 2018-11-13 19:32:27 +0000 | [diff] [blame] | 515 | if (is_contained(Config.KeepSection, Sec.Name)) |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 516 | return false; |
| 517 | // Otherwise defer to RemovePred. |
| 518 | return RemovePred(Sec); |
| 519 | }; |
| 520 | } |
| 521 | |
| 522 | // This has to be the last predicate assignment. |
| 523 | // If the option --keep-symbol has been specified |
| 524 | // and at least one of those symbols is present |
| 525 | // (equivalently, the updated symbol table is not empty) |
| 526 | // the symbol table and the string table should not be removed. |
| 527 | if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) && |
| 528 | Obj.SymbolTable && !Obj.SymbolTable->empty()) { |
| 529 | RemovePred = [&Obj, RemovePred](const SectionBase &Sec) { |
| 530 | if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab()) |
| 531 | return false; |
| 532 | return RemovePred(Sec); |
| 533 | }; |
| 534 | } |
| 535 | |
| 536 | if (Config.CompressionType != DebugCompressionType::None) |
Fangrui Song | 3dfc3fb | 2019-03-15 10:27:28 +0000 | [diff] [blame] | 537 | replaceDebugSections(Obj, RemovePred, isCompressable, |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 538 | [&Config, &Obj](const SectionBase *S) { |
| 539 | return &Obj.addSection<CompressedSection>( |
| 540 | *S, Config.CompressionType); |
| 541 | }); |
| 542 | else if (Config.DecompressDebugSections) |
| 543 | replaceDebugSections( |
Fangrui Song | 3dfc3fb | 2019-03-15 10:27:28 +0000 | [diff] [blame] | 544 | Obj, RemovePred, |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 545 | [](const SectionBase &S) { return isa<CompressedSection>(&S); }, |
| 546 | [&Obj](const SectionBase *S) { |
| 547 | auto CS = cast<CompressedSection>(S); |
| 548 | return &Obj.addSection<DecompressedSection>(*CS); |
| 549 | }); |
| 550 | |
James Henderson | 66a9d0f | 2019-04-18 09:13:30 +0000 | [diff] [blame] | 551 | return Obj.removeSections(Config.AllowBrokenLinks, RemovePred); |
George Rimar | e6963be | 2019-03-25 12:34:25 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | // This function handles the high level operations of GNU objcopy including |
| 555 | // handling command line options. It's important to outline certain properties |
| 556 | // we expect to hold of the command line operations. Any operation that "keeps" |
| 557 | // should keep regardless of a remove. Additionally any removal should respect |
| 558 | // any previous removals. Lastly whether or not something is removed shouldn't |
| 559 | // depend a) on the order the options occur in or b) on some opaque priority |
| 560 | // system. The only priority is that keeps/copies overrule removes. |
| 561 | static Error handleArgs(const CopyConfig &Config, Object &Obj, |
| 562 | const Reader &Reader, ElfType OutputElfType) { |
| 563 | |
| 564 | if (!Config.SplitDWO.empty()) |
| 565 | if (Error E = |
| 566 | splitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType)) |
| 567 | return E; |
| 568 | |
| 569 | if (Config.OutputArch) { |
| 570 | Obj.Machine = Config.OutputArch.getValue().EMachine; |
| 571 | Obj.OSABI = Config.OutputArch.getValue().OSABI; |
| 572 | } |
| 573 | |
George Rimar | 279898b | 2019-03-26 18:42:15 +0000 | [diff] [blame] | 574 | // It is important to remove the sections first. For example, we want to |
| 575 | // remove the relocation sections before removing the symbols. That allows |
| 576 | // us to avoid reporting the inappropriate errors about removing symbols |
| 577 | // named in relocations. |
| 578 | if (Error E = replaceAndRemoveSections(Config, Obj)) |
George Rimar | e6963be | 2019-03-25 12:34:25 +0000 | [diff] [blame] | 579 | return E; |
| 580 | |
George Rimar | 279898b | 2019-03-26 18:42:15 +0000 | [diff] [blame] | 581 | if (Error E = updateAndRemoveSymbols(Config, Obj)) |
Jordan Rupprecht | 971d4762 | 2019-02-01 15:20:36 +0000 | [diff] [blame] | 582 | return E; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 583 | |
James Henderson | fa11fb3 | 2019-05-08 09:49:35 +0000 | [diff] [blame^] | 584 | if (!Config.SectionsToRename.empty() || !Config.AllocSectionsPrefix.empty()) { |
| 585 | DenseSet<SectionBase *> PrefixedSections; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 586 | for (auto &Sec : Obj.sections()) { |
| 587 | const auto Iter = Config.SectionsToRename.find(Sec.Name); |
| 588 | if (Iter != Config.SectionsToRename.end()) { |
| 589 | const SectionRename &SR = Iter->second; |
| 590 | Sec.Name = SR.NewName; |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 591 | if (SR.NewFlags.hasValue()) |
Jordan Rupprecht | 017deaf | 2019-04-02 16:49:56 +0000 | [diff] [blame] | 592 | setSectionFlagsAndType(Sec, SR.NewFlags.getValue()); |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 593 | } |
James Henderson | fa11fb3 | 2019-05-08 09:49:35 +0000 | [diff] [blame^] | 594 | |
| 595 | // Add a prefix to allocated sections and their relocation sections. This |
| 596 | // should be done after renaming the section by Config.SectionToRename to |
| 597 | // imitate the GNU objcopy behavior. |
| 598 | if (!Config.AllocSectionsPrefix.empty()) { |
| 599 | if (Sec.Flags & SHF_ALLOC) { |
| 600 | Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str(); |
| 601 | PrefixedSections.insert(&Sec); |
| 602 | |
| 603 | // Rename relocation sections associated to the allocated sections. |
| 604 | // For example, if we rename .text to .prefix.text, we also rename |
| 605 | // .rel.text to .rel.prefix.text. |
| 606 | // |
| 607 | // Dynamic relocation sections (SHT_REL[A] with SHF_ALLOC) are handled |
| 608 | // above, e.g., .rela.plt is renamed to .prefix.rela.plt, not |
| 609 | // .rela.prefix.plt since GNU objcopy does so. |
| 610 | } else if (auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) { |
| 611 | auto *TargetSec = RelocSec->getSection(); |
| 612 | if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) { |
| 613 | StringRef prefix; |
| 614 | switch (Sec.Type) { |
| 615 | case SHT_REL: |
| 616 | prefix = ".rel"; |
| 617 | break; |
| 618 | case SHT_RELA: |
| 619 | prefix = ".rela"; |
| 620 | break; |
| 621 | default: |
| 622 | continue; |
| 623 | } |
| 624 | |
| 625 | // If the relocation section comes *after* the target section, we |
| 626 | // don't add Config.AllocSectionsPrefix because we've already added |
| 627 | // the prefix to TargetSec->Name. Otherwise, if the relocation |
| 628 | // section comes *before* the target section, we add the prefix. |
| 629 | if (PrefixedSections.count(TargetSec)) { |
| 630 | Sec.Name = (prefix + TargetSec->Name).str(); |
| 631 | } else { |
| 632 | const auto Iter = Config.SectionsToRename.find(TargetSec->Name); |
| 633 | if (Iter != Config.SectionsToRename.end()) { |
| 634 | // Both `--rename-section` and `--prefix-alloc-sections` are |
| 635 | // given but the target section is not yet renamed. |
| 636 | Sec.Name = |
| 637 | (prefix + Config.AllocSectionsPrefix + Iter->second.NewName) |
| 638 | .str(); |
| 639 | } else { |
| 640 | Sec.Name = |
| 641 | (prefix + Config.AllocSectionsPrefix + TargetSec->Name) |
| 642 | .str(); |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | } |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
| 651 | if (!Config.SetSectionFlags.empty()) { |
| 652 | for (auto &Sec : Obj.sections()) { |
| 653 | const auto Iter = Config.SetSectionFlags.find(Sec.Name); |
| 654 | if (Iter != Config.SetSectionFlags.end()) { |
| 655 | const SectionFlagsUpdate &SFU = Iter->second; |
Jordan Rupprecht | 017deaf | 2019-04-02 16:49:56 +0000 | [diff] [blame] | 656 | setSectionFlagsAndType(Sec, SFU.NewFlags); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | } |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 660 | |
Eugene Leviant | c76671b | 2019-03-12 12:41:06 +0000 | [diff] [blame] | 661 | for (const auto &Flag : Config.AddSection) { |
| 662 | std::pair<StringRef, StringRef> SecPair = Flag.split("="); |
| 663 | StringRef SecName = SecPair.first; |
| 664 | StringRef File = SecPair.second; |
| 665 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = |
| 666 | MemoryBuffer::getFile(File); |
| 667 | if (!BufOrErr) |
| 668 | return createFileError(File, errorCodeToError(BufOrErr.getError())); |
| 669 | std::unique_ptr<MemoryBuffer> Buf = std::move(*BufOrErr); |
| 670 | ArrayRef<uint8_t> Data( |
| 671 | reinterpret_cast<const uint8_t *>(Buf->getBufferStart()), |
| 672 | Buf->getBufferSize()); |
| 673 | OwnedDataSection &NewSection = |
| 674 | Obj.addSection<OwnedDataSection>(SecName, Data); |
| 675 | if (SecName.startswith(".note") && SecName != ".note.GNU-stack") |
| 676 | NewSection.Type = SHT_NOTE; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Eugene Leviant | c76671b | 2019-03-12 12:41:06 +0000 | [diff] [blame] | 679 | for (const auto &Flag : Config.DumpSection) { |
| 680 | std::pair<StringRef, StringRef> SecPair = Flag.split("="); |
| 681 | StringRef SecName = SecPair.first; |
| 682 | StringRef File = SecPair.second; |
| 683 | if (Error E = dumpSectionToFile(SecName, File, Obj)) |
| 684 | return createFileError(Config.InputFilename, std::move(E)); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | if (!Config.AddGnuDebugLink.empty()) |
| 688 | Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink); |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 689 | |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 690 | for (const NewSymbolInfo &SI : Config.SymbolsToAdd) { |
| 691 | SectionBase *Sec = Obj.findSection(SI.SectionName); |
| 692 | uint64_t Value = Sec ? Sec->Addr + SI.Value : SI.Value; |
Simon Pilgrim | 65706cf | 2019-02-27 10:19:53 +0000 | [diff] [blame] | 693 | Obj.SymbolTable->addSymbol( |
| 694 | SI.SymbolName, SI.Bind, SI.Type, Sec, Value, SI.Visibility, |
| 695 | Sec ? (uint16_t)SYMBOL_SIMPLE_INDEX : (uint16_t)SHN_ABS, 0); |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Eugene Leviant | 53350d0 | 2019-02-26 09:24:22 +0000 | [diff] [blame] | 698 | if (Config.EntryExpr) |
| 699 | Obj.Entry = Config.EntryExpr(Obj.Entry); |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 700 | return Error::success(); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 703 | Error executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In, |
| 704 | Buffer &Out) { |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 705 | BinaryReader Reader(Config.BinaryArch, &In); |
| 706 | std::unique_ptr<Object> Obj = Reader.create(); |
| 707 | |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 708 | // Prefer OutputArch (-O<format>) if set, otherwise fallback to BinaryArch |
| 709 | // (-B<arch>). |
| 710 | const ElfType OutputElfType = getOutputElfType( |
| 711 | Config.OutputArch ? Config.OutputArch.getValue() : Config.BinaryArch); |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 712 | if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType)) |
| 713 | return E; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 714 | std::unique_ptr<Writer> Writer = |
| 715 | createWriter(Config, *Obj, Out, OutputElfType); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 716 | if (Error E = Writer->finalize()) |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 717 | return E; |
| 718 | return Writer->write(); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 721 | Error executeObjcopyOnBinary(const CopyConfig &Config, |
| 722 | object::ELFObjectFileBase &In, Buffer &Out) { |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 723 | ELFReader Reader(&In); |
| 724 | std::unique_ptr<Object> Obj = Reader.create(); |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 725 | // Prefer OutputArch (-O<format>) if set, otherwise infer it from the input. |
| 726 | const ElfType OutputElfType = |
| 727 | Config.OutputArch ? getOutputElfType(Config.OutputArch.getValue()) |
| 728 | : getOutputElfType(In); |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 729 | ArrayRef<uint8_t> BuildIdBytes; |
| 730 | |
| 731 | if (!Config.BuildIdLinkDir.empty()) { |
| 732 | BuildIdBytes = unwrapOrError(findBuildID(In)); |
| 733 | if (BuildIdBytes.size() < 2) |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 734 | return createFileError( |
| 735 | Config.InputFilename, |
| 736 | createStringError(object_error::parse_failed, |
| 737 | "build ID is smaller than two bytes.")); |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Jordan Rupprecht | fc832e9 | 2019-01-30 18:13:30 +0000 | [diff] [blame] | 740 | if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkInput) |
| 741 | if (Error E = |
| 742 | linkToBuildIdDir(Config, Config.InputFilename, |
| 743 | Config.BuildIdLinkInput.getValue(), BuildIdBytes)) |
| 744 | return E; |
| 745 | |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 746 | if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType)) |
| 747 | return E; |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 748 | std::unique_ptr<Writer> Writer = |
| 749 | createWriter(Config, *Obj, Out, OutputElfType); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 750 | if (Error E = Writer->finalize()) |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 751 | return E; |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 752 | if (Error E = Writer->write()) |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 753 | return E; |
Jordan Rupprecht | fc832e9 | 2019-01-30 18:13:30 +0000 | [diff] [blame] | 754 | if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkOutput) |
| 755 | if (Error E = |
| 756 | linkToBuildIdDir(Config, Config.OutputFilename, |
| 757 | Config.BuildIdLinkOutput.getValue(), BuildIdBytes)) |
| 758 | return E; |
| 759 | |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 760 | return Error::success(); |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | } // end namespace elf |
| 764 | } // end namespace objcopy |
| 765 | } // end namespace llvm |