llvm.org GIT mirror llvm / 9cf90e6
[yaml2obj] Remove --format option in favor of YAML tags Summary: Our YAML library's handling of tags isn't perfect, but it is good enough to get rid of the need for the --format argument to yaml2obj. This patch does exactly that. Instead of requiring --format, it infers the format based on the tags found in the object file. The supported tags are: !ELF !COFF !mach-o !fat-mach-o I have a corresponding patch that is quite large that fixes up all the in-tree test cases. Reviewers: rafael, Bigcheese, compnerd, silvas Subscribers: compnerd, llvm-commits Differential Revision: http://reviews.llvm.org/D21711 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273915 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Bieneman 3 years ago
76 changed file(s) with 261 addition(s) and 208 deletion(s). Raw diff Collapse all Expand all
128128 std::vector Slices;
129129 };
130130
131 struct MachFile {
132 bool isFat;
133 UniversalBinary FatFile;
134 Object ThinFile;
135 };
136
137131 } // namespace llvm::MachOYAML
138132 } // namespace llvm
139133
171165
172166 template <> struct MappingTraits {
173167 static void mapping(IO &IO, MachOYAML::UniversalBinary &UniversalBinary);
174 };
175
176 template <> struct MappingTraits {
177 static void mapping(IO &IO, MachOYAML::MachFile &MachFile);
178168 };
179169
180170 template <> struct MappingTraits {
0 //===- ObjectYAML.h ---------------------------------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_OBJECTYAML_OBJECTYAML_H
10 #define LLVM_OBJECTYAML_OBJECTYAML_H
11
12 #include "llvm/Support/YAMLTraits.h"
13 #include "llvm/ObjectYAML/ELFYAML.h"
14 #include "llvm/ObjectYAML/COFFYAML.h"
15 #include "llvm/ObjectYAML/MachOYAML.h"
16
17 namespace llvm {
18 namespace yaml {
19
20 struct YamlObjectFile {
21 std::unique_ptr Elf;
22 std::unique_ptr Coff;
23 std::unique_ptr MachO;
24 std::unique_ptr FatMachO;
25 };
26
27 template <> struct MappingTraits {
28 static void mapping(IO &IO, YamlObjectFile &ObjectFile);
29 };
30
31 } // namespace yaml
32 } // namespace llvm
33
34 #endif
22 COFFYAML.cpp
33 ELFYAML.cpp
44 MachOYAML.cpp
5 ObjectYAML.cpp
56 )
492492 }
493493
494494 void MappingTraits::mapping(IO &IO, COFFYAML::Object &Obj) {
495 IO.mapTag("!COFF", true);
495496 IO.mapOptional("OptionalHeader", Obj.OptionalHeader);
496497 IO.mapRequired("header", Obj.Header);
497498 IO.mapRequired("sections", Obj.Sections);
819819 void MappingTraits::mapping(IO &IO, ELFYAML::Object &Object) {
820820 assert(!IO.getContext() && "The IO context is initialized already");
821821 IO.setContext(&Object);
822 IO.mapTag("!ELF", true);
822823 IO.mapRequired("FileHeader", Object.Header);
823824 IO.mapOptional("Sections", Object.Sections);
824825 IO.mapOptional("Symbols", Object.Symbols);
128128 IO.mapRequired("Slices", UniversalBinary.Slices);
129129
130130 if (IO.getContext() == &UniversalBinary)
131 IO.setContext(nullptr);
132 }
133
134 void MappingTraits::mapping(
135 IO &IO, MachOYAML::MachFile &MachFile) {
136 if (!IO.getContext()) {
137 IO.setContext(&MachFile);
138 }
139 if (IO.outputting()) {
140 if (MachFile.isFat) {
141 IO.mapTag("!fat-mach-o", true);
142 MappingTraits::mapping(IO, MachFile.FatFile);
143 } else {
144 IO.mapTag("!mach-o", true);
145 MappingTraits::mapping(IO, MachFile.ThinFile);
146 }
147 } else {
148 if (IO.mapTag("!fat-mach-o")) {
149 MachFile.isFat = true;
150 MappingTraits::mapping(IO, MachFile.FatFile);
151 } else if (IO.mapTag("!mach-o")) {
152 MachFile.isFat = false;
153 MappingTraits::mapping(IO, MachFile.ThinFile);
154 } else {
155 assert(false && "No tag found in YAML, cannot identify file type!");
156 }
157 }
158 if (IO.getContext() == &MachFile)
159131 IO.setContext(nullptr);
160132 }
161133
0 //===- ObjectYAML.cpp - YAML utilities for object files -------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines a wrapper class for handling tagged YAML input
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/ObjectYAML/YAML.h"
14 #include "llvm/ObjectYAML/ObjectYAML.h"
15
16 using namespace llvm;
17 using namespace yaml;
18
19 void MappingTraits::mapping(IO &IO,
20 YamlObjectFile &ObjectFile) {
21 if (IO.outputting()) {
22 if (ObjectFile.Elf)
23 MappingTraits::mapping(IO, *ObjectFile.Elf);
24 if (ObjectFile.Coff)
25 MappingTraits::mapping(IO, *ObjectFile.Coff);
26 if (ObjectFile.MachO)
27 MappingTraits::mapping(IO, *ObjectFile.MachO);
28 if (ObjectFile.FatMachO)
29 MappingTraits::mapping(IO,
30 *ObjectFile.FatMachO);
31 } else {
32 if (IO.mapTag("!ELF")) {
33 ObjectFile.Elf.reset(new ELFYAML::Object());
34 MappingTraits::mapping(IO, *ObjectFile.Elf);
35 } else if (IO.mapTag("!COFF")) {
36 ObjectFile.Coff.reset(new COFFYAML::Object());
37 MappingTraits::mapping(IO, *ObjectFile.Coff);
38 } else if (IO.mapTag("!mach-o")) {
39 ObjectFile.MachO.reset(new MachOYAML::Object());
40 MappingTraits::mapping(IO, *ObjectFile.MachO);
41 } else if (IO.mapTag("!fat-mach-o")) {
42 ObjectFile.FatMachO.reset(new MachOYAML::UniversalBinary());
43 MappingTraits::mapping(IO,
44 *ObjectFile.FatMachO);
45 } else {
46 Input &In = (Input &)IO;
47 std::string Tag = In.getCurrentNode()->getRawTag();
48 if (Tag.empty())
49 IO.setError("YAML Object File missing document type tag!");
50 else
51 IO.setError(
52 llvm::Twine("YAML Object File unsupported document type tag '") +
53 llvm::Twine(Tag.c_str()) + llvm::Twine("'!"));
54 }
55 }
56 }
None # RUN: yaml2obj -format=elf %s > %t
0 # RUN: yaml2obj %s > %t
11 # RUN: obj2yaml %t | FileCheck %s
22
33 # CHECK: - Name: .rela.text
1313 # CHECK-NEXT: Symbol: main
1414 # CHECK-NEXT: Type: R_AARCH64_TLSGD_ADR_PREL21
1515
16 !ELF
1617 FileHeader:
1718 Class: ELFCLASS64
1819 Data: ELFDATA2LSB
None # RUN: yaml2obj -format=elf %s > %t.o
0 # RUN: yaml2obj %s > %t.o
11 # RUN: llvm-readobj -s -file-headers %t.o | FileCheck %s
22
33 # CHECK: Format: ELF64-amdgpu-hsacobj
1313 # CHECK: SHF_AMDGPU_HSA_READONLY (0x200000)
1414 # CHECK: }
1515
16 ---
16 --- !ELF
1717 FileHeader:
1818 Class: ELFCLASS64
1919 Data: ELFDATA2LSB
None # RUN: yaml2obj -format=elf %s > %t.o
0 # RUN: yaml2obj %s > %t.o
11 # RUN: llvm-readobj -s -file-headers %t.o | FileCheck %s
22
33 # CHECK: Format: ELF32-amdgpu
44
5 ---
5 --- !ELF
66 FileHeader:
77 Class: ELFCLASS32
88 Data: ELFDATA2LSB
None # RUN: yaml2obj -format=elf %s > %t
0 # RUN: yaml2obj %s > %t
11 # RUN: llvm-readobj -r %t | FileCheck %s
22
33 # CHECK: Relocations [
1212 # CHECK: }
1313 # CHECK: ]
1414
15 !ELF
1516 FileHeader:
1617 Class: ELFCLASS64
1718 Data: ELFDATA2LSB
None # RUN: yaml2obj -format=elf %s > %t.o
0 # RUN: yaml2obj %s > %t.o
11 # RUN: llvm-readobj -s -file-headers %t.o | FileCheck %s
22
33 # CHECK: Format: ELF64-amdgpu
0 !COFF
1 header: !Header
12 Machine: IMAGE_FILE_MACHINE_I386 # (0x14c)
23 Characteristics: [ IMAGE_FILE_DEBUG_STRIPPED ]
None ---
0 --- !COFF
11 header:
22 Machine: IMAGE_FILE_MACHINE_AMD64
33 Characteristics: [ IMAGE_FILE_RELOCS_STRIPPED, IMAGE_FILE_LINE_NUMS_STRIPPED ]
None ---
0 --- !COFF
11 header:
22 Machine: IMAGE_FILE_MACHINE_I386
33 Characteristics: [ IMAGE_FILE_RELOCS_STRIPPED, IMAGE_FILE_LINE_NUMS_STRIPPED, IMAGE_FILE_LOCAL_SYMS_STRIPPED, IMAGE_FILE_32BIT_MACHINE ]
None ---
0 --- !COFF
11 header:
22 Machine: IMAGE_FILE_MACHINE_I386
33 Characteristics: [ IMAGE_FILE_RELOCS_STRIPPED, IMAGE_FILE_EXECUTABLE_IMAGE, IMAGE_FILE_LINE_NUMS_STRIPPED, IMAGE_FILE_32BIT_MACHINE, IMAGE_FILE_DEBUG_STRIPPED ]
None ---
0 --- !COFF
11 header:
22 Machine: IMAGE_FILE_MACHINE_I386
33 Characteristics: [ IMAGE_FILE_LINE_NUMS_STRIPPED, IMAGE_FILE_32BIT_MACHINE ]
None ---
0 --- !COFF
11 header:
22 Machine: IMAGE_FILE_MACHINE_ARMNT
33 Characteristics: [ ]
0 !COFF
1 header: !Header
12 Machine: IMAGE_FILE_MACHINE_AMD64 # (0x8664)
23
None # RUN: yaml2obj -format=elf %s > %t
0 # RUN: yaml2obj %s > %t
11 # RUN: llvm-readobj -r %t | FileCheck %s
22
33 # CHECK: Relocations [
1212 # CHECK-NEXT: }
1313 # CHECK-NEXT: ]
1414
15 !ELF
1516 FileHeader:
1617 Class: ELFCLASS32
1718 Data: ELFDATA2LSB
None # RUN: yaml2obj -format=elf %s > %t
0 # RUN: yaml2obj %s > %t
11 # RUN: llvm-readobj -mips-abi-flags %t | FileCheck -check-prefix=OBJ %s
22 # RUN: obj2yaml %t | FileCheck -check-prefix=YAML %s
33
3333 # YAML-NEXT: CPR1Size: REG_64
3434 # YAML-NEXT: Flags1: [ ODDSPREG ]
3535
36 !ELF
3637 FileHeader:
3738 Class: ELFCLASS64
3839 Data: ELFDATA2MSB
None # RUN: yaml2obj -format=elf -docnum=1 %s > %t.o32
0 # RUN: yaml2obj -docnum=1 %s > %t.o32
11 # RUN: llvm-readobj -file-headers %t.o32 | FileCheck -check-prefix=O32OBJ %s
22 # RUN: obj2yaml %t.o32 | FileCheck -check-prefix=O32YAML %s
33
99
1010 # O32YAML: Flags: [ EF_MIPS_PIC, EF_MIPS_ABI_O32, EF_MIPS_ARCH_32 ]
1111
12 # RUN: yaml2obj -format=elf -docnum=2 %s > %t.o64
12 # RUN: yaml2obj -docnum=2 %s > %t.o64
1313 # RUN: llvm-readobj -file-headers %t.o64 | FileCheck -check-prefix=O64OBJ %s
1414 # RUN: obj2yaml %t.o64 | FileCheck -check-prefix=O64YAML %s
1515
2121
2222 # O64YAML: Flags: [ EF_MIPS_PIC, EF_MIPS_ABI_O64, EF_MIPS_ARCH_64 ]
2323
24 # RUN: yaml2obj -format=elf -docnum=3 %s > %t.eabi32
24 # RUN: yaml2obj -docnum=3 %s > %t.eabi32
2525 # RUN: llvm-readobj -file-headers %t.eabi32 | FileCheck -check-prefix=E32OBJ %s
2626 # RUN: obj2yaml %t.eabi32 | FileCheck -check-prefix=E32YAML %s
2727
3333
3434 # E32YAML: Flags: [ EF_MIPS_PIC, EF_MIPS_ABI_EABI32, EF_MIPS_ARCH_32 ]
3535
36 # RUN: yaml2obj -format=elf -docnum=4 %s > %t.eabi64
36 # RUN: yaml2obj -docnum=4 %s > %t.eabi64
3737 # RUN: llvm-readobj -file-headers %t.eabi64 | FileCheck -check-prefix=E64OBJ %s
3838 # RUN: obj2yaml %t.eabi64 | FileCheck -check-prefix=E64YAML %s
3939
4646 # E64YAML: Flags: [ EF_MIPS_PIC, EF_MIPS_ABI_EABI64, EF_MIPS_ARCH_64 ]
4747
4848 # o32
49 ---
49 --- !ELF
5050 FileHeader:
5151 Class: ELFCLASS32
5252 Data: ELFDATA2LSB
None # RUN: yaml2obj -format=elf %s > %t
0 # RUN: yaml2obj %s > %t
11 # RUN: llvm-readobj -file-headers %t | FileCheck -check-prefix=OBJ %s
22 # RUN: obj2yaml %t | FileCheck -check-prefix=YAML %s
33
2424 # YAML-NEXT: Machine: EM_MIPS
2525 # YAML-NEXT: Flags: [ EF_MIPS_NOREORDER, EF_MIPS_PIC, EF_MIPS_CPIC, EF_MIPS_ABI2, EF_MIPS_32BITMODE, EF_MIPS_FP64, EF_MIPS_NAN2008, EF_MIPS_MICROMIPS, EF_MIPS_ARCH_ASE_M16, EF_MIPS_ARCH_ASE_MDMX, EF_MIPS_ABI_O32, EF_MIPS_MACH_OCTEON, EF_MIPS_ARCH_32R6 ]
2626
27 ---
27 --- !ELF
2828 FileHeader:
2929 Class: ELFCLASS32
3030 Data: ELFDATA2LSB
None # RUN: yaml2obj -format=elf %s > %t
0 # RUN: yaml2obj %s > %t
11 # RUN: llvm-readobj -r %t | FileCheck -check-prefix=OBJ %s
22 # RUN: obj2yaml %t | FileCheck -check-prefix=YAML %s
33
4040 # YAML-NEXT: Type2: R_MIPS_LO16
4141 # YAML-NEXT: SpecSym: RSS_GP0
4242
43 ---
43 --- !ELF
4444 FileHeader:
4545 Class: ELFCLASS64
4646 Data: ELFDATA2LSB
None # RUN: yaml2obj -format=elf %s > %t
0 # RUN: yaml2obj %s > %t
11 # RUN: llvm-readobj -r %t | FileCheck %s
22
33 # CHECK: Relocations [
66 # CHECK-NEXT: }
77 # CHECK-NEXT: ]
88
9 !ELF
910 FileHeader:
1011 Class: ELFCLASS32
1112 Data: ELFDATA2LSB
None # RUN: yaml2obj -format=elf %s | llvm-readobj -file-headers - | FileCheck %s
0 # RUN: yaml2obj %s | llvm-readobj -file-headers - | FileCheck %s
11
22 !ELF
33 FileHeader: !FileHeader
0 # Checks that the tool is able to read section groups with ELF.
11 RUN: obj2yaml %p/Inputs/sectionGroup.elf.x86-64 > %t1.sectiongroup.yaml
22 RUN: FileCheck %s --check-prefix ELF-GROUP < %t1.sectiongroup.yaml
3 RUN: yaml2obj -format=elf %t1.sectiongroup.yaml -o %t2.o.elf
3 RUN: yaml2obj %t1.sectiongroup.yaml -o %t2.o.elf
44 RUN: llvm-readobj -sections %t2.o.elf | FileCheck %s -check-prefix=SECTIONS
55 #ELF-GROUP: - Name: .group
66 #ELF-GROUP: Type: SHT_GROUP
11
22 # CHECK: Section alignment is too large
33
4 ---
4 --- !COFF
55 header:
66 Machine: IMAGE_FILE_MACHINE_AMD64
77 Characteristics: []
None # RUN: yaml2obj -format=coff -docnum=1 %s \
0 # RUN: yaml2obj -docnum=1 %s \
11 # RUN: | llvm-readobj -symbols - | FileCheck -check-prefix=DOC1 %s
2 # RUN: yaml2obj -format=coff -docnum=2 %s \
2 # RUN: yaml2obj -docnum=2 %s \
33 # RUN: | llvm-readobj -symbols - | FileCheck -check-prefix=DOC2 %s
4 # RUN: not yaml2obj -format=coff -docnum=3 %s 2>&1 \
4 # RUN: not yaml2obj -docnum=3 %s 2>&1 \
55 # RUN: | FileCheck -check-prefix=DOC3 %s
66
77 # DOC1: Name: _sym1
88 # DOC2: Name: _sym2
99 # DOC3: yaml2obj: Cannot find the 3rd document
1010
11 ---
11 --- !COFF
1212 header:
1313 Machine: IMAGE_FILE_MACHINE_I386
1414 Characteristics: [ IMAGE_FILE_DEBUG_STRIPPED ]
4848 ComplexType: IMAGE_SYM_DTYPE_NULL
4949 StorageClass: IMAGE_SYM_CLASS_EXTERNAL
5050
51 ---
51 --- !COFF
5252 header:
5353 Machine: IMAGE_FILE_MACHINE_I386
5454 Characteristics: [ IMAGE_FILE_DEBUG_STRIPPED ]
0 # Check that yaml2obj takes in account section AddressAlign field.
11
2 # RUN: yaml2obj -format=elf %s > %t
2 # RUN: yaml2obj %s > %t
33 # RUN: llvm-readobj -s %t | FileCheck %s
44
55 # CHECK: Section {
1919 # CHECK-NEXT: EntrySize: 0
2020 # CHECK-NEXT: }
2121
22 ---
22 --- !ELF
2323 FileHeader:
2424 Class: ELFCLASS32
2525 Data: ELFDATA2LSB
None RUN: yaml2obj -format=elf %p/Inputs/ELF/LE64.yaml | llvm-readobj -file-headers - | FileCheck %s --check-prefix LE64
1 RUN: yaml2obj -format=elf %p/Inputs/ELF/BE64.yaml | llvm-readobj -file-headers - | FileCheck %s --check-prefix BE64
2 RUN: yaml2obj -format=elf %p/Inputs/ELF/LE32.yaml | llvm-readobj -file-headers - | FileCheck %s --check-prefix LE32
3 RUN: yaml2obj -format=elf %p/Inputs/ELF/BE32.yaml | llvm-readobj -file-headers - | FileCheck %s --check-prefix BE32
0 RUN: yaml2obj %p/Inputs/ELF/LE64.yaml | llvm-readobj -file-headers - | FileCheck %s --check-prefix LE64
1 RUN: yaml2obj %p/Inputs/ELF/BE64.yaml | llvm-readobj -file-headers - | FileCheck %s --check-prefix BE64
2 RUN: yaml2obj %p/Inputs/ELF/LE32.yaml | llvm-readobj -file-headers - | FileCheck %s --check-prefix LE32
3 RUN: yaml2obj %p/Inputs/ELF/BE32.yaml | llvm-readobj -file-headers - | FileCheck %s --check-prefix BE32
44
55 LE64: Class: 64-bit (0x2)
66 LE64-NEXT: DataEncoding: LittleEndian (0x1)
None # RUN: yaml2obj -format=elf %s | llvm-readobj -file-headers - | FileCheck %s
0 # RUN: yaml2obj %s | llvm-readobj -file-headers - | FileCheck %s
11 !ELF
22 FileHeader:
33 Class: ELFCLASS32
None # RUN: yaml2obj -format=elf %s | llvm-readobj -file-headers - | FileCheck %s
0 # RUN: yaml2obj %s | llvm-readobj -file-headers - | FileCheck %s
11 !ELF
22 FileHeader:
33 Class: ELFCLASS64
None # RUN: yaml2obj -format=elf -docnum=1 %s \
0 # RUN: yaml2obj -docnum=1 %s \
11 # RUN: | llvm-readobj -symbols - | FileCheck -check-prefix=DOC1 %s
2 # RUN: yaml2obj -format=elf -docnum=2 %s \
2 # RUN: yaml2obj -docnum=2 %s \
33 # RUN: | llvm-readobj -symbols - | FileCheck -check-prefix=DOC2 %s
4 # RUN: not yaml2obj -format=elf -docnum=3 %s 2>&1 \
4 # RUN: not yaml2obj -docnum=3 %s 2>&1 \
55 # RUN: | FileCheck -check-prefix=DOC3 %s
66
77 # DOC1: Name: T1 (1)
None # RUN: yaml2obj -format=elf %s | llvm-readobj -sections -relocations - | FileCheck %s
0 # RUN: yaml2obj %s | llvm-readobj -sections -relocations - | FileCheck %s
11
22 !ELF
33 FileHeader: !FielHeader
None # RUN: yaml2obj -format=elf %s | llvm-readobj -sections -relocations - | FileCheck %s
0 # RUN: yaml2obj %s | llvm-readobj -sections -relocations - | FileCheck %s
11
22 !ELF
33 FileHeader: !FileHeader
None # RUN: yaml2obj -format=elf %s | llvm-readobj -sections -section-data - | FileCheck %s
1 # RUN: yaml2obj -format=elf -o %t %s
0 # RUN: yaml2obj %s | llvm-readobj -sections -section-data - | FileCheck %s
1 # RUN: yaml2obj -o %t %s
22 # RUN: llvm-readobj -sections -section-data %t | FileCheck %s
33
44 !ELF
None # RUN: not yaml2obj -format=elf -o %t %s 2>&1 | FileCheck %s
0 # RUN: not yaml2obj -o %t %s 2>&1 | FileCheck %s
11
22 !ELF
33 FileHeader:
None # RUN: yaml2obj -format=elf %s | llvm-readobj -symbols - | FileCheck %s
0 # RUN: yaml2obj %s | llvm-readobj -symbols - | FileCheck %s
11 !ELF
22 FileHeader:
33 Class: ELFCLASS64
None # RUN: yaml2obj -format=elf %s | llvm-readobj -symbols - | FileCheck %s
0 # RUN: yaml2obj %s | llvm-readobj -symbols - | FileCheck %s
11 !ELF
22 FileHeader:
33 Class: ELFCLASS64
None # RUN: yaml2obj -format=elf %s | llvm-readobj -symbols - | \
0 # RUN: yaml2obj %s | llvm-readobj -symbols - | \
11 # RUN: FileCheck --check-prefix OBJ %s
2 # RUN: yaml2obj -format=elf %s | obj2yaml - | FileCheck --check-prefix YAML %s
2 # RUN: yaml2obj %s | obj2yaml - | FileCheck --check-prefix YAML %s
33
44 # OBJ: Symbol {
55 # OBJ: Name: default1 (36)
8686 # YAML-NEXT: Visibility: STV_PROTECTED
8787 # YAML-NEXT: Other: [ STO_MIPS_PIC, STO_MIPS_MICROMIPS ]
8888
89 ---
89 --- !ELF
9090 FileHeader:
9191 Class: ELFCLASS32
9292 Data: ELFDATA2LSB
None # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
11
22 --- !mach-o
33 FileHeader:
None # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
11
22 --- !mach-o
33 FileHeader:
None # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
11
22 --- !mach-o
33 FileHeader:
None # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
11
22 --- !mach-o
33 FileHeader:
None # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
11
22 --- !fat-mach-o
33 FatHeader:
None # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
11
22 --- !mach-o
33 FileHeader:
None # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
11
22 --- !mach-o
33 FileHeader:
None # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
11
22 --- !mach-o
33 FileHeader:
None # RUN: not yaml2obj -format=macho %s 2>&1 | FileCheck %s
0 # RUN: not yaml2obj %s 2>&1 | FileCheck %s
11
22 --- !mach-o
33 FileHeader:
None # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
11
22 --- !mach-o
33 FileHeader:
None # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
11
22 --- !mach-o
33 FileHeader:
None # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
11
22 --- !mach-o
33 FileHeader:
None # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
11
22 --- !mach-o
33 FileHeader:
None # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
11
22 --- !mach-o
33 FileHeader:
None # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
11
22 --- !mach-o
33 FileHeader:
None ---
0 --- !COFF
11 header:
22 Machine: IMAGE_FILE_MACHINE_AMD64
33 Characteristics: [ ]
None ---
0 --- !ELF
11 FileHeader:
22 Class: ELFCLASS64
33 Data: ELFDATA2LSB
None RUN: yaml2obj -format macho %S/Inputs/macho.yaml -o %t-macho.o
1 RUN: yaml2obj -format coff %S/Inputs/coff.yaml -o %t-coff.o
0 RUN: yaml2obj %S/Inputs/macho.yaml -o %t-macho.o
1 RUN: yaml2obj %S/Inputs/coff.yaml -o %t-coff.o
22
33 RUN: rm -f %t.ar
44 RUN: llvm-ar crs %t.ar %t-macho.o
None RUN: yaml2obj -format coff %S/Inputs/coff.yaml -o %t.obj
0 RUN: yaml2obj %S/Inputs/coff.yaml -o %t.obj
11
22 RUN: rm -f %t.ar
33 RUN: llvm-ar crs %t.ar %t.obj
None RUN: yaml2obj -format elf %S/Inputs/elf.yaml -o %t.o
0 RUN: yaml2obj %S/Inputs/elf.yaml -o %t.o
11
22 RUN: rm -f %t.ar
33 RUN: llvm-ar crs %t.ar %t.o
None RUN: yaml2obj -format macho %S/Inputs/macho.yaml -o %t.o
0 RUN: yaml2obj %S/Inputs/macho.yaml -o %t.o
11
22 RUN: rm -f %t.ar
33 RUN: llvm-ar crs %t.ar %t.o
None RUN: yaml2obj -format macho %S/Inputs/macho.yaml -o %t.o
0 RUN: yaml2obj %S/Inputs/macho.yaml -o %t.o
11
22 RUN: rm -f %t.ar
33 RUN: llvm-ar -format gnu crs %t.ar %t.o
0 !COFF
1 header: !Header
12 Machine: IMAGE_FILE_MACHINE_I386 # (0x14c)
23 Characteristics: [ IMAGE_FILE_DEBUG_STRIPPED ]
0 !COFF
1 header: !Header
12 Machine: IMAGE_FILE_MACHINE_I386 # (0x14c)
23 Characteristics: [ IMAGE_FILE_DEBUG_STRIPPED ]
0 !COFF
1 header: !Header
12 Machine: IMAGE_FILE_MACHINE_I386 # (0x14c)
23 Characteristics: [ IMAGE_FILE_DEBUG_STRIPPED ]
0 # Check that llvm-readobj shows arch specific ELF section flags.
11
2 # RUN: yaml2obj -format=elf -docnum 1 %s > %t-amdgpu.o
2 # RUN: yaml2obj -docnum 1 %s > %t-amdgpu.o
33 # RUN: llvm-readobj -s %t-amdgpu.o | FileCheck -check-prefix=AMD %s
44
55 # AMD: Flags [ (0x300000)
88 # AMD-NEXT: ]
99
1010 # amdgpu.o
11 ---
11 --- !ELF
1212 FileHeader:
1313 Class: ELFCLASS64
1414 Data: ELFDATA2LSB
2222 Flags: [SHF_AMDGPU_HSA_GLOBAL, SHF_AMDGPU_HSA_READONLY]
2323 Size: 4
2424
25 # RUN: yaml2obj -format=elf -docnum 2 %s > %t-hex.o
25 # RUN: yaml2obj -docnum 2 %s > %t-hex.o
2626 # RUN: llvm-readobj -s %t-hex.o | FileCheck -check-prefix=HEX %s
2727
2828 # HEX: Flags [ (0x10000000)
3030 # HEX-NEXT: ]
3131
3232 # hex.o
33 ---
33 --- !ELF
3434 FileHeader:
3535 Class: ELFCLASS32
3636 Data: ELFDATA2LSB
4343 Flags: [SHF_HEX_GPREL]
4444 Size: 4
4545
46 # RUN: yaml2obj -format=elf -docnum 3 %s > %t-mips.o
46 # RUN: yaml2obj -docnum 3 %s > %t-mips.o
4747 # RUN: llvm-readobj -s %t-mips.o | FileCheck -check-prefix=MIPS %s
4848
4949 # MIPS: Flags [ (0x38000000)
5353 # MIPS-NEXT: ]
5454
5555 # mips.o
56 ---
56 --- !ELF
5757 FileHeader:
5858 Class: ELFCLASS32
5959 Data: ELFDATA2LSB
6666 Flags: [SHF_MIPS_GPREL, SHF_MIPS_MERGE, SHF_MIPS_NOSTRIP]
6767 Size: 4
6868
69 # RUN: yaml2obj -format=elf -docnum 4 %s > %t-x86_64.o
69 # RUN: yaml2obj -docnum 4 %s > %t-x86_64.o
7070 # RUN: llvm-readobj -s %t-x86_64.o | FileCheck -check-prefix=X86_64 %s
7171
7272 # X86_64: Flags [ (0x10000000)
7474 # X86_64-NEXT: ]
7575
7676 # x86_64.o
77 ---
77 --- !ELF
7878 FileHeader:
7979 Class: ELFCLASS64
8080 Data: ELFDATA2LSB
None # RUN: yaml2obj -format=elf %s > %t1.o
1 # RUN: yaml2obj -format=elf %s > %t2.o
0 # RUN: yaml2obj %s > %t1.o
1 # RUN: yaml2obj %s > %t2.o
22
33 # RUN: echo -ne "\x04" > %t.stats
44
3838 # CHECK: /tmp{{[/\\]}}f.c:2 f2 cfi-icall 12
3939 # CHECK: /tmp{{[/\\]}}f.c:1 f1 14
4040
41 ---
41 --- !ELF
4242 FileHeader:
4343 Class: ELFCLASS64
4444 Data: ELFDATA2LSB
0 # RUN: not yaml2obj %s 2>&1 | FileCheck %s
1
2 ---
3 DummyData:
4 foo: 0
5 ...
0 # RUN: not yaml2obj %s 2>&1 | FileCheck %s
1
2 --- !unsupported-tag
3 DummyData:
4 foo: 0
5 ...
6
7 #check error: YAML Object File unsupported document type tag '!unsupported-tag'!
99 #include "Error.h"
1010 #include "obj2yaml.h"
1111 #include "llvm/Object/MachOUniversal.h"
12 #include "llvm/ObjectYAML/MachOYAML.h"
12 #include "llvm/ObjectYAML/ObjectYAML.h"
1313 #include "llvm/Support/ErrorHandling.h"
1414 #include "llvm/Support/LEB128.h"
1515
466466 if (!YAML)
467467 return YAML.takeError();
468468
469 yaml::YamlObjectFile YAMLFile;
470 YAMLFile.MachO = std::move(YAML.get());
471
469472 yaml::Output Yout(Out);
470 Yout << *(YAML.get());
473 Yout << YAMLFile;
471474 return Error::success();
472475 }
473476
474477 Error macho2yaml(raw_ostream &Out, const object::MachOUniversalBinary &Obj) {
475 MachOYAML::MachFile YAMLFile;
476 YAMLFile.isFat = true;
477 MachOYAML::UniversalBinary &YAML = YAMLFile.FatFile;
478 yaml::YamlObjectFile YAMLFile;
479 YAMLFile.FatMachO.reset(new MachOYAML::UniversalBinary());
480 MachOYAML::UniversalBinary &YAML = *YAMLFile.FatMachO;
478481 YAML.Header.magic = Obj.getMagic();
479482 YAML.Header.nfat_arch = Obj.getNumberOfObjects();
480483
1717 #include "llvm/ADT/StringMap.h"
1818 #include "llvm/ADT/StringSwitch.h"
1919 #include "llvm/Object/COFF.h"
20 #include "llvm/ObjectYAML/COFFYAML.h"
20 #include "llvm/ObjectYAML/ObjectYAML.h"
2121 #include "llvm/Support/Endian.h"
2222 #include "llvm/Support/MemoryBuffer.h"
2323 #include "llvm/Support/SourceMgr.h"
531531 return true;
532532 }
533533
534 int yaml2coff(yaml::Input &YIn, raw_ostream &Out) {
535 COFFYAML::Object Doc;
536 YIn >> Doc;
537 if (YIn.error()) {
538 errs() << "yaml2obj: Failed to parse YAML file!\n";
539 return 1;
540 }
541
534 int yaml2coff(llvm::COFFYAML::Object &Doc, raw_ostream &Out) {
542535 COFFParser CP(Doc);
543536 if (!CP.parse()) {
544537 errs() << "yaml2obj: Failed to parse YAML file!\n";
557557 return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
558558 }
559559
560 int yaml2elf(yaml::Input &YIn, raw_ostream &Out) {
561 ELFYAML::Object Doc;
562 YIn >> Doc;
563 if (YIn.error()) {
564 errs() << "yaml2obj: Failed to parse YAML file!\n";
565 return 1;
566 }
560 int yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out) {
567561 using object::ELFType;
568562 typedef ELFType LE64;
569563 typedef ELFType BE64;
1212 //===----------------------------------------------------------------------===//
1313
1414 #include "yaml2obj.h"
15 #include "llvm/ObjectYAML/MachOYAML.h"
15 #include "llvm/ObjectYAML/ObjectYAML.h"
1616 #include "llvm/Support/Error.h"
1717 #include "llvm/Support/LEB128.h"
1818 #include "llvm/Support/MachO.h"
425425
426426 class UniversalWriter {
427427 public:
428 UniversalWriter(MachOYAML::MachFile &MachFile)
429 : MachFile(MachFile), fileStart(0) {}
428 UniversalWriter(yaml::YamlObjectFile &ObjectFile)
429 : ObjectFile(ObjectFile), fileStart(0) {}
430430
431431 Error writeMachO(raw_ostream &OS);
432432
436436
437437 void ZeroToOffset(raw_ostream &OS, size_t offset);
438438
439 MachOYAML::MachFile &MachFile;
439 yaml::YamlObjectFile &ObjectFile;
440440 uint64_t fileStart;
441441 };
442442
443443 Error UniversalWriter::writeMachO(raw_ostream &OS) {
444444 fileStart = OS.tell();
445 if (!MachFile.isFat) {
446 MachOWriter Writer(MachFile.ThinFile);
445 if (ObjectFile.MachO) {
446 MachOWriter Writer(*ObjectFile.MachO);
447447 return Writer.writeMachO(OS);
448448 }
449449 if (auto Err = writeFatHeader(OS))
450450 return Err;
451451 if (auto Err = writeFatArchs(OS))
452452 return Err;
453 auto &FatFile = MachFile.FatFile;
453 auto &FatFile = *ObjectFile.FatMachO;
454454 assert(FatFile.FatArchs.size() == FatFile.Slices.size());
455455 for (size_t i = 0; i < FatFile.Slices.size(); i++) {
456456 ZeroToOffset(OS, FatFile.FatArchs[i].offset);
464464 }
465465
466466 Error UniversalWriter::writeFatHeader(raw_ostream &OS) {
467 auto &FatFile = MachFile.FatFile;
467 auto &FatFile = *ObjectFile.FatMachO;
468468 MachO::fat_header header;
469469 header.magic = FatFile.Header.magic;
470470 header.nfat_arch = FatFile.Header.nfat_arch;
508508 }
509509
510510 Error UniversalWriter::writeFatArchs(raw_ostream &OS) {
511 auto &FatFile = MachFile.FatFile;
511 auto &FatFile = *ObjectFile.FatMachO;
512512 bool is64Bit = FatFile.Header.magic == MachO::FAT_MAGIC_64;
513513 for (auto Arch : FatFile.FatArchs) {
514514 if (is64Bit)
528528
529529 } // end anonymous namespace
530530
531 int yaml2macho(yaml::Input &YIn, raw_ostream &Out) {
532 MachOYAML::MachFile Doc;
533 YIn >> Doc;
534 if (YIn.error()) {
535 errs() << "yaml2obj: Failed to parse YAML file!\n";
536 return 1;
537 }
538
531 int yaml2macho(yaml::YamlObjectFile &Doc, raw_ostream &Out) {
539532 UniversalWriter Writer(Doc);
540533 if (auto Err = Writer.writeMachO(Out)) {
541534 errs() << toString(std::move(Err));
1515
1616 #include "yaml2obj.h"
1717 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ObjectYAML/ObjectYAML.h"
1819 #include "llvm/Support/CommandLine.h"
1920 #include "llvm/Support/FileSystem.h"
2021 #include "llvm/Support/ManagedStatic.h"
3132 static cl::opt
3233 Input(cl::Positional, cl::desc(""), cl::init("-"));
3334
34 // TODO: The "right" way to tell what kind of object file a given YAML file
35 // corresponds to is to look at YAML "tags" (e.g. `!Foo`). Then, different
36 // tags (`!ELF`, `!COFF`, etc.) would be used to discriminate between them.
37 // Interpreting the tags is needed eventually for when writing test cases,
38 // so that we can e.g. have `!Archive` contain a sequence of `!ELF`, and
39 // just Do The Right Thing. However, interpreting these tags and acting on
40 // them appropriately requires some work in the YAML parser and the YAMLIO
41 // library.
42 enum YAMLObjectFormat {
43 YOF_COFF,
44 YOF_ELF,
45 YOF_MACHO
46 };
47
48 cl::opt Format(
49 "format",
50 cl::desc("Interpret input as this type of object file"),
51 cl::values(
52 clEnumValN(YOF_COFF, "coff", "COFF object file format"),
53 clEnumValN(YOF_ELF, "elf", "ELF object file format"),
54 clEnumValN(YOF_MACHO, "macho", "Mach-O object file format"),
55 clEnumValEnd));
56
5735 cl::opt
5836 DocNum("docnum", cl::init(1),
5937 cl::desc("Read specified document from input (default = 1)"));
6139 static cl::opt OutputFilename("o", cl::desc("Output filename"),
6240 cl::value_desc("filename"));
6341
64 typedef int (*ConvertFuncPtr)(yaml::Input & YIn, raw_ostream &Out);
65
66 static int convertYAML(yaml::Input &YIn, raw_ostream &Out,
67 ConvertFuncPtr Convert) {
42 static int convertYAML(yaml::Input &YIn, raw_ostream &Out) {
6843 unsigned CurDocNum = 0;
6944 do {
70 if (++CurDocNum == DocNum)
71 return Convert(YIn, Out);
45 if (++CurDocNum == DocNum) {
46 yaml::YamlObjectFile Doc;
47 YIn >> Doc;
48 if (YIn.error()) {
49 errs() << "yaml2obj: Failed to parse YAML file!\n";
50 return 1;
51 }
52
53 if (Doc.Elf)
54 return yaml2elf(*Doc.Elf, Out);
55 if (Doc.Coff)
56 return yaml2coff(*Doc.Coff, Out);
57 if (Doc.MachO || Doc.FatMachO)
58 return yaml2macho(Doc, Out);
59 errs() << "yaml2obj: Unknown document type!\n";
60 return 1;
61 }
7262 } while (YIn.nextDocument());
7363
7464 errs() << "yaml2obj: Cannot find the " << DocNum
9888 if (!Buf)
9989 return 1;
10090
101 ConvertFuncPtr Convert = nullptr;
102 if (Format == YOF_COFF)
103 Convert = yaml2coff;
104 else if (Format == YOF_ELF)
105 Convert = yaml2elf;
106 else if (Format == YOF_MACHO)
107 Convert = yaml2macho;
108 else {
109 errs() << "Not yet implemented\n";
110 return 1;
111 }
112
11391 yaml::Input YIn(Buf.get()->getBuffer());
11492
115 int Res = convertYAML(YIn, Out->os(), Convert);
93 int Res = convertYAML(YIn, Out->os());
11694 if (Res == 0)
11795 Out->keep();
11896
1313
1414 namespace llvm {
1515 class raw_ostream;
16
17 namespace COFFYAML {
18 struct Object;
19 }
20
21 namespace ELFYAML {
22 struct Object;
23 }
24
1625 namespace yaml {
1726 class Input;
27 struct YamlObjectFile;
1828 }
1929 }
20 int yaml2coff(llvm::yaml::Input &YIn, llvm::raw_ostream &Out);
21 int yaml2elf(llvm::yaml::Input &YIn, llvm::raw_ostream &Out);
22 int yaml2macho(llvm::yaml::Input &YIn, llvm::raw_ostream &Out);
30
31 int yaml2coff(llvm::COFFYAML::Object &Doc, llvm::raw_ostream &Out);
32 int yaml2elf(llvm::ELFYAML::Object &Doc, llvm::raw_ostream &Out);
33 int yaml2macho(llvm::yaml::YamlObjectFile &Doc, llvm::raw_ostream &Out);
2334
2435 #endif