llvm.org GIT mirror llvm / f6dc13f
[llvm-objcopy] Ignore -B --binary-architecture= GNU objcopy documents that -B is only useful with architecture-less input (i.e. "binary" or "ihex"). After D67144, -O defaults to -I, and -B is essentially a NOP. * If -O is binary/ihex, GNU objcopy ignores -B. * If -O is elf*, -B provides the e_machine field in GNU objcopy. So to convert a blob to an ELF, `-I binary -B i386:x86-64 -O elf64-x86-64` has to be specified. `-I binary -B i386:x86-64 -O elf64-x86-64` creates an ELF with its e_machine field set to EM_NONE in GNU objcopy, but a regular x86_64 ELF in elftoolchain elfcopy. Follow the elftoolchain approach (ignoring -B) to simplify code. Users that expect their command line portable should specify -B. Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D67215 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@371914 91177308-0d34-0410-b5e6-96231b3b80d8 Fangrui Song 30 days ago
8 changed file(s) with 18 addition(s) and 72 deletion(s). Raw diff Collapse all Expand all
4141 output. For ELF objects the section will be of type `SHT_NOTE`, if the name
4242 starts with ".note". Otherwise, it will have type `SHT_PROGBITS`. Can be
4343 specified multiple times to add multiple sections.
44
45 .. option:: --binary-architecture , -B
46
47 Ignored for compatibility.
4448
4549 .. option:: --disable-deterministic-archives, -U
4650
179183
180184 Allow llvm-objcopy to remove sections even if it would leave invalid section
181185 references. Any invalid sh_link fields will be set to zero.
182
183 .. option:: --binary-architecture , -B
184
185 Specify the architecture to use, when transforming an architecture-less format
186 (e.g. binary) to another format. Valid options are:
187
188 - `aarch64`
189 - `arm`
190 - `i386`
191 - `i386:x86-64`
192 - `mips`
193 - `powerpc:common64`
194 - `riscv:rv32`
195 - `riscv:rv64`
196 - `sparc`
197 - `sparcel`
198 - `x86-64`
199186
200187 .. option:: --build-id-link-dir
201188
0 # RUN: echo abcd > %t.txt
11
2 # RUN: not llvm-objcopy -I binary -B xyz %t.txt %t.o 2>&1 \
3 # RUN: | FileCheck %s --check-prefix=BAD-BINARY-ARCH
4
5 # BAD-BINARY-ARCH: invalid architecture: 'xyz'
2 ## -B is ignored.
3 # RUN: llvm-objcopy -I binary -B xyz %t.txt %t.o
255255 UnsupportedFlags.size() > 1 ? "s" : "",
256256 join(UnsupportedFlags, "', '").c_str());
257257 return SI;
258 }
259
260 static const StringMap ArchMap{
261 // Name, {EMachine, 64bit, LittleEndian}
262 {"aarch64", {ELF::EM_AARCH64, true, true}},
263 {"arm", {ELF::EM_ARM, false, true}},
264 {"i386", {ELF::EM_386, false, true}},
265 {"i386:x86-64", {ELF::EM_X86_64, true, true}},
266 {"mips", {ELF::EM_MIPS, false, false}},
267 {"powerpc:common64", {ELF::EM_PPC64, true, true}},
268 {"riscv:rv32", {ELF::EM_RISCV, false, true}},
269 {"riscv:rv64", {ELF::EM_RISCV, true, true}},
270 {"sparc", {ELF::EM_SPARC, false, false}},
271 {"sparcel", {ELF::EM_SPARC, false, true}},
272 {"x86-64", {ELF::EM_X86_64, true, true}},
273 };
274
275 static Expected getMachineInfo(StringRef Arch) {
276 auto Iter = ArchMap.find(Arch);
277 if (Iter == std::end(ArchMap))
278 return createStringError(errc::invalid_argument,
279 "invalid architecture: '%s'", Arch.str().c_str());
280 return Iter->getValue();
281258 }
282259
283260 struct TargetInfo {
488465 .Case("binary", FileFormat::Binary)
489466 .Case("ihex", FileFormat::IHex)
490467 .Default(FileFormat::Unspecified);
491 if (Config.InputFormat == FileFormat::Binary) {
492 auto BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);
493 if (!BinaryArch.empty()) {
494 Expected MI = getMachineInfo(BinaryArch);
495 if (!MI)
496 return MI.takeError();
497 Config.BinaryArch = *MI;
498 }
499 }
500468
501469 if (opt::Arg *A = InputArgs.getLastArg(OBJCOPY_new_symbol_visibility)) {
502470 const uint8_t Invalid = 0xff;
127127 StringRef OutputFilename;
128128 FileFormat OutputFormat;
129129
130 // Only applicable for --input-format=binary
131 MachineInfo BinaryArch;
132130 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
133131 Optional OutputArch;
134132
736736 IHexReader Reader(&In);
737737 std::unique_ptr Obj = Reader.create();
738738 const ElfType OutputElfType =
739 getOutputElfType(Config.OutputArch.getValueOr(Config.BinaryArch));
739 getOutputElfType(Config.OutputArch.getValueOr(MachineInfo()));
740740 if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType))
741741 return E;
742742 return writeOutput(Config, *Obj, Out, OutputElfType);
746746 Buffer &Out) {
747747 uint8_t NewSymbolVisibility =
748748 Config.NewSymbolVisibility.getValueOr((uint8_t)ELF::STV_DEFAULT);
749 BinaryReader Reader(Config.BinaryArch, &In, NewSymbolVisibility);
749 BinaryReader Reader(&In, NewSymbolVisibility);
750750 std::unique_ptr Obj = Reader.create();
751751
752752 // Prefer OutputArch (-O) if set, otherwise fallback to BinaryArch
753753 // (-B).
754754 const ElfType OutputElfType =
755 getOutputElfType(Config.OutputArch.getValueOr(Config.BinaryArch));
755 getOutputElfType(Config.OutputArch.getValueOr(MachineInfo()));
756756 if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType))
757757 return E;
758758 return writeOutput(Config, *Obj, Out, OutputElfType);
11111111 Obj->OSABI = ELFOSABI_NONE;
11121112 Obj->ABIVersion = 0;
11131113 Obj->Entry = 0x0;
1114 Obj->Machine = EMachine;
1114 Obj->Machine = EM_NONE;
11151115 Obj->Version = 1;
11161116 }
11171117
16051605 Reader::~Reader() {}
16061606
16071607 std::unique_ptr BinaryReader::create() const {
1608 return BinaryELFBuilder(MInfo.EMachine, MemBuf, NewSymbolVisibility).build();
1608 return BinaryELFBuilder(MemBuf, NewSymbolVisibility).build();
16091609 }
16101610
16111611 Expected> IHexReader::parse() const {
872872
873873 class BasicELFBuilder {
874874 protected:
875 uint16_t EMachine;
876875 std::unique_ptr Obj;
877876
878877 void initFileHeader();
882881 void initSections();
883882
884883 public:
885 BasicELFBuilder(uint16_t EM)
886 : EMachine(EM), Obj(std::make_unique()) {}
884 BasicELFBuilder() : Obj(std::make_unique()) {}
887885 };
888886
889887 class BinaryELFBuilder : public BasicELFBuilder {
892890 void addData(SymbolTableSection *SymTab);
893891
894892 public:
895 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB, uint8_t NewSymbolVisibility)
896 : BasicELFBuilder(EM), MemBuf(MB),
893 BinaryELFBuilder(MemoryBuffer *MB, uint8_t NewSymbolVisibility)
894 : BasicELFBuilder(), MemBuf(MB),
897895 NewSymbolVisibility(NewSymbolVisibility) {}
898896
899897 std::unique_ptr build();
906904
907905 public:
908906 IHexELFBuilder(const std::vector &Records)
909 : BasicELFBuilder(ELF::EM_386), Records(Records) {}
907 : BasicELFBuilder(), Records(Records) {}
910908
911909 std::unique_ptr build();
912910 };
941939 };
942940
943941 class BinaryReader : public Reader {
944 const MachineInfo &MInfo;
945942 MemoryBuffer *MemBuf;
946943 uint8_t NewSymbolVisibility;
947944
948945 public:
949 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB,
950 const uint8_t NewSymbolVisibility)
951 : MInfo(MI), MemBuf(MB), NewSymbolVisibility(NewSymbolVisibility) {}
946 BinaryReader(MemoryBuffer *MB, const uint8_t NewSymbolVisibility)
947 : MemBuf(MB), NewSymbolVisibility(NewSymbolVisibility) {}
952948 std::unique_ptr create() const override;
953949 };
954950
0 include "CommonOpts.td"
11
22 defm binary_architecture
3 : Eq<"binary-architecture", "Used when transforming an architecture-less "
4 "format (such as binary) to another format">;
3 : Eq<"binary-architecture", "Ignored for compatibility">;
54 def B : JoinedOrSeparate<["-"], "B">,
65 Alias,
76 HelpText<"Alias for --binary-architecture">;