llvm.org GIT mirror llvm / df96c56
[yaml2obj] Add new command line option `-docnum`. Input YAML file might contain multiple object file definitions. New option `-docnum` allows to specify an ordinal number (starting from 1) of definition used for an object file generation. Patch reviewed by Sean Silva. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@209967 91177308-0d34-0410-b5e6-96231b3b80d8 Simon Atanasyan 5 years ago
8 changed file(s) with 187 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
981981 // These are only used by operator>>. They could be private
982982 // if those templated things could be made friends.
983983 bool setCurrentDocument();
984 void nextDocument();
984 bool nextDocument();
985985
986986 private:
987987 llvm::SourceMgr SrcMgr; // must be before Strm
8989 return false;
9090 }
9191
92 void Input::nextDocument() {
93 ++DocIterator;
92 bool Input::nextDocument() {
93 return ++DocIterator != Strm->end();
9494 }
9595
9696 bool Input::mapTag(StringRef Tag, bool Default) {
0 # RUN: yaml2obj -format=coff -docnum=1 %s \
1 # RUN: | llvm-readobj -symbols - | FileCheck -check-prefix=DOC1 %s
2 # RUN: yaml2obj -format=coff -docnum=2 %s \
3 # RUN: | llvm-readobj -symbols - | FileCheck -check-prefix=DOC2 %s
4 # RUN: not yaml2obj -format=coff -docnum=3 %s 2>&1 \
5 # RUN: | FileCheck -check-prefix=DOC3 %s
6
7 # DOC1: Name: _sym1
8 # DOC2: Name: _sym2
9 # DOC3: yaml2obj: Cannot find the 3rd document
10
11 ---
12 header:
13 Machine: IMAGE_FILE_MACHINE_I386
14 Characteristics: [ IMAGE_FILE_DEBUG_STRIPPED ]
15
16 sections:
17 - Name: .text
18 Alignment: 16
19 Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE,
20 IMAGE_SCN_MEM_READ ]
21 SectionData: "00000000"
22
23 symbols:
24 - Name: .text
25 Value: 0
26 SectionNumber: 1
27 SimpleType: IMAGE_SYM_TYPE_NULL
28 ComplexType: IMAGE_SYM_DTYPE_NULL
29 StorageClass: IMAGE_SYM_CLASS_STATIC
30 SectionDefinition:
31 Length: 36
32 NumberOfRelocations: 3
33 NumberOfLinenumbers: 0
34 CheckSum: 0
35 Number: 1
36
37 - Name: _main
38 Value: 0
39 SectionNumber: 1
40 SimpleType: IMAGE_SYM_TYPE_NULL
41 ComplexType: IMAGE_SYM_DTYPE_FUNCTION
42 StorageClass: IMAGE_SYM_CLASS_EXTERNAL
43
44 - Name: _sym1
45 Value: 0
46 SectionNumber: 0
47 SimpleType: IMAGE_SYM_TYPE_NULL
48 ComplexType: IMAGE_SYM_DTYPE_NULL
49 StorageClass: IMAGE_SYM_CLASS_EXTERNAL
50
51 ---
52 header:
53 Machine: IMAGE_FILE_MACHINE_I386
54 Characteristics: [ IMAGE_FILE_DEBUG_STRIPPED ]
55
56 sections:
57 - Name: .text
58 Alignment: 16
59 Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE,
60 IMAGE_SCN_MEM_READ ]
61 SectionData: "00000000"
62
63 symbols:
64 - Name: .text
65 Value: 0
66 SectionNumber: 1
67 SimpleType: IMAGE_SYM_TYPE_NULL
68 ComplexType: IMAGE_SYM_DTYPE_NULL
69 StorageClass: IMAGE_SYM_CLASS_STATIC
70 SectionDefinition:
71 Length: 36
72 NumberOfRelocations: 3
73 NumberOfLinenumbers: 0
74 CheckSum: 0
75 Number: 1
76
77 - Name: _main
78 Value: 0
79 SectionNumber: 1
80 SimpleType: IMAGE_SYM_TYPE_NULL
81 ComplexType: IMAGE_SYM_DTYPE_FUNCTION
82 StorageClass: IMAGE_SYM_CLASS_EXTERNAL
83
84 - Name: _sym2
85 Value: 0
86 SectionNumber: 0
87 SimpleType: IMAGE_SYM_TYPE_NULL
88 ComplexType: IMAGE_SYM_DTYPE_NULL
89 StorageClass: IMAGE_SYM_CLASS_EXTERNAL
90 ...
0 # RUN: yaml2obj -format=elf -docnum=1 %s \
1 # RUN: | llvm-readobj -symbols - | FileCheck -check-prefix=DOC1 %s
2 # RUN: yaml2obj -format=elf -docnum=2 %s \
3 # RUN: | llvm-readobj -symbols - | FileCheck -check-prefix=DOC2 %s
4 # RUN: not yaml2obj -format=elf -docnum=3 %s 2>&1 \
5 # RUN: | FileCheck -check-prefix=DOC3 %s
6
7 # DOC1: Name: T1 (1)
8 # DOC2: Name: T2 (1)
9 # DOC3: yaml2obj: Cannot find the 3rd document
10
11 --- !ELF
12 FileHeader: !FileHeader
13 Class: ELFCLASS32
14 Data: ELFDATA2LSB
15 Type: ET_REL
16 Machine: EM_MIPS
17 Flags: [EF_MIPS_CPIC]
18
19 Sections:
20 - Name: .text
21 Type: SHT_PROGBITS
22 Content: "0000000000000000"
23 AddressAlign: 16
24 Flags: [SHF_EXECINSTR, SHF_ALLOC]
25
26 Symbols:
27 Global:
28 - Name: T1
29 Section: .text
30 Type: STT_FUNC
31 Value: 0x0
32 Size: 8
33
34 --- !ELF
35 FileHeader: !FileHeader
36 Class: ELFCLASS32
37 Data: ELFDATA2LSB
38 Type: ET_REL
39 Machine: EM_MIPS
40
41 Sections:
42 - Name: .text
43 Type: SHT_PROGBITS
44 Content: "00000000"
45 AddressAlign: 16
46 Flags: [SHF_EXECINSTR, SHF_ALLOC]
47
48 Symbols:
49 Global:
50 - Name: T2
51 Section: .text
52 Type: STT_FUNC
53 Value: 0x0
54 Size: 4
55 ...
326326 return true;
327327 }
328328
329 int yaml2coff(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) {
330 yaml::Input YIn(Buf->getBuffer());
329 int yaml2coff(yaml::Input &YIn, raw_ostream &Out) {
331330 COFFYAML::Object Doc;
332331 YIn >> Doc;
333332 if (YIn.error()) {
466466 return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
467467 }
468468
469 int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) {
470 yaml::Input YIn(Buf->getBuffer());
469 int yaml2elf(yaml::Input &YIn, raw_ostream &Out) {
471470 ELFYAML::Object Doc;
472471 YIn >> Doc;
473472 if (YIn.error()) {
1414 //===----------------------------------------------------------------------===//
1515
1616 #include "yaml2obj.h"
17 #include "llvm/ADT/StringExtras.h"
1718 #include "llvm/Support/CommandLine.h"
1819 #include "llvm/Support/FileSystem.h"
1920 #include "llvm/Support/ManagedStatic.h"
2324 #include "llvm/Support/raw_ostream.h"
2425 #include "llvm/Support/system_error.h"
2526 #include "llvm/Support/ToolOutputFile.h"
27 #include "llvm/Support/YAMLTraits.h"
2628
2729 using namespace llvm;
2830
5052 clEnumValN(YOF_ELF, "elf", "ELF object file format"),
5153 clEnumValEnd));
5254
55 cl::opt
56 DocNum("docnum", cl::init(1),
57 cl::desc("Read specified document from input (default = 1)"));
58
5359 static cl::opt OutputFilename("o", cl::desc("Output filename"),
5460 cl::value_desc("filename"));
61
62 typedef int (*ConvertFuncPtr)(yaml::Input & YIn, raw_ostream &Out);
63
64 int convertYAML(yaml::Input & YIn, raw_ostream &Out, ConvertFuncPtr Convert) {
65 unsigned CurDocNum = 0;
66 do {
67 if (++CurDocNum == DocNum)
68 return Convert(YIn, Out);
69 } while (YIn.nextDocument());
70
71 errs() << "yaml2obj: Cannot find the " << DocNum
72 << llvm::getOrdinalSuffix(DocNum) << " document\n";
73 return 1;
74 }
5575
5676 int main(int argc, char **argv) {
5777 cl::ParseCommandLineOptions(argc, argv);
7494 if (MemoryBuffer::getFileOrSTDIN(Input, Buf))
7595 return 1;
7696
77 int Res = 1;
97 ConvertFuncPtr Convert = nullptr;
7898 if (Format == YOF_COFF)
79 Res = yaml2coff(Out->os(), Buf.get());
99 Convert = yaml2coff;
80100 else if (Format == YOF_ELF)
81 Res = yaml2elf(Out->os(), Buf.get());
82 else
101 Convert = yaml2elf;
102 else {
83103 errs() << "Not yet implemented\n";
104 return 1;
105 }
84106
107 yaml::Input YIn(Buf->getBuffer());
108
109 int Res = convertYAML(YIn, Out->os(), Convert);
85110 if (Res == 0)
86111 Out->keep();
87112
1212 #define LLVM_TOOLS_YAML2OBJ_H
1313
1414 namespace llvm {
15 class raw_ostream;
16 class MemoryBuffer;
15 class raw_ostream;
16 namespace yaml {
17 class Input;
1718 }
18 int yaml2coff(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf);
19 int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf);
19 }
20 int yaml2coff(llvm::yaml::Input &YIn, llvm::raw_ostream &Out);
21 int yaml2elf(llvm::yaml::Input &YIn, llvm::raw_ostream &Out);
2022
2123 #endif