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