llvm.org GIT mirror llvm / ac5b917
Revert "[obj2yaml] [yaml2obj] Support for MachO Load Command data" This reverts commit r270115. This failed on several builders using GCC. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@270121 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Bieneman 3 years ago
4 changed file(s) with 31 addition(s) and 170 deletion(s). Raw diff Collapse all Expand all
5151 virtual ~LoadCommand();
5252 llvm::MachO::macho_load_command Data;
5353 std::vector
Sections;
54 std::vector PayloadBytes;
55 std::string PayloadString;
56 uint64_t ZeroPadBytes;
5754 };
5855
5956 struct Object {
6764
6865 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::LoadCommand)
6966 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Section)
70 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::Hex8)
7167
7268 namespace llvm {
7369 namespace yaml {
9595 IO.setContext(nullptr);
9696 }
9797
98 template
99 void mapLoadCommandData(IO &IO, MachOYAML::LoadCommand &LoadCommand) {}
100
101 template <>
102 void mapLoadCommandData(
103 IO &IO, MachOYAML::LoadCommand &LoadCommand) {
104 IO.mapOptional("Sections", LoadCommand.Sections);
105 }
106
107 template <>
108 void mapLoadCommandData(
109 IO &IO, MachOYAML::LoadCommand &LoadCommand) {
110 IO.mapOptional("Sections", LoadCommand.Sections);
111 }
112
113 template <>
114 void mapLoadCommandData(
115 IO &IO, MachOYAML::LoadCommand &LoadCommand) {
116 IO.mapOptional("PayloadString", LoadCommand.PayloadString);
117 }
118
119 template <>
120 void mapLoadCommandData(
121 IO &IO, MachOYAML::LoadCommand &LoadCommand) {
122 IO.mapOptional("PayloadString", LoadCommand.PayloadString);
123 }
124
12598 void MappingTraits::mapping(
12699 IO &IO, MachOYAML::LoadCommand &LoadCommand) {
127100 IO.mapRequired(
132105 case MachO::LCName: \
133106 MappingTraits::mapping(IO, \
134107 LoadCommand.Data.LCStruct##_data); \
135 mapLoadCommandData(IO, LoadCommand); \
136108 break;
137109
138110 switch (LoadCommand.Data.load_command_data.cmd) {
139111 #include "llvm/Support/MachO.def"
140112 }
141 IO.mapOptional("PayloadBytes", LoadCommand.PayloadBytes);
142 IO.mapOptional("ZeroPadBytes", LoadCommand.ZeroPadBytes, 0ull);
113 if (LoadCommand.Data.load_command_data.cmd == MachO::LC_SEGMENT ||
114 LoadCommand.Data.load_command_data.cmd == MachO::LC_SEGMENT_64) {
115 IO.mapOptional("Sections", LoadCommand.Sections);
116 }
143117 }
144118
145119 void MappingTraits::mapping(
1818
1919 class MachODumper {
2020
21 template
22 const char *processLoadCommandData(
23 MachOYAML::LoadCommand &LC,
24 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd);
25
2621 const object::MachOObjectFile &Obj;
2722
2823 public:
3631 sizeof(MachO::LCStruct)); \
3732 if (Obj.isLittleEndian() != sys::IsLittleEndianHost) \
3833 MachO::swapStruct(LC.Data.LCStruct##_data); \
39 EndPtr = processLoadCommandData(LC, LoadCmd); \
4034 break;
4135
4236 template
7367 }
7468
7569 template
76 const char *
77 extractSections(const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
78 std::vector &Sections,
79 bool IsLittleEndian) {
70 void extractSections(
71 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
72 std::vector &Sections, bool IsLittleEndian) {
8073 auto End = LoadCmd.Ptr + LoadCmd.C.cmdsize;
8174 const SectionType *Curr =
8275 reinterpret_cast(LoadCmd.Ptr + sizeof(SegmentType));
9083 Sections.push_back(constructSection(*Curr));
9184 }
9285 }
93 return reinterpret_cast(Curr);
94 }
95
96 template
97 const char *MachODumper::processLoadCommandData(
98 MachOYAML::LoadCommand &LC,
99 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
100 return LoadCmd.Ptr + sizeof(StructType);
101 }
102
103 template <>
104 const char *MachODumper::processLoadCommandData(
105 MachOYAML::LoadCommand &LC,
106 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
107 return extractSections(
108 LoadCmd, LC.Sections, Obj.isLittleEndian());
109 }
110
111 template <>
112 const char *MachODumper::processLoadCommandData(
113 MachOYAML::LoadCommand &LC,
114 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
115 return extractSections(
116 LoadCmd, LC.Sections, Obj.isLittleEndian());
117 }
118
119 template
120 const char *
121 readString(MachOYAML::LoadCommand &LC,
122 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
123 auto Start = LoadCmd.Ptr + sizeof(StructType);
124 auto MaxSize = LoadCmd.C.cmdsize - sizeof(StructType);
125 auto Size = strnlen(Start, MaxSize);
126 LC.PayloadString = StringRef(Start, Size).str();
127 return Start + Size;
128 }
129
130 template <>
131 const char *MachODumper::processLoadCommandData(
132 MachOYAML::LoadCommand &LC,
133 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
134 return readString(LC, LoadCmd);
135 }
136
137 template <>
138 const char *MachODumper::processLoadCommandData(
139 MachOYAML::LoadCommand &LC,
140 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
141 return readString(LC, LoadCmd);
14286 }
14387
14488 Expected> MachODumper::dump() {
15498
15599 for (auto LoadCmd : Obj.load_commands()) {
156100 MachOYAML::LoadCommand LC;
157 const char *EndPtr = LoadCmd.Ptr;
158101 switch (LoadCmd.C.cmd) {
159102 default:
160103 memcpy((void *)&(LC.Data.load_command_data), LoadCmd.Ptr,
161104 sizeof(MachO::load_command));
162105 if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
163106 MachO::swapStruct(LC.Data.load_command_data);
164 EndPtr = processLoadCommandData(LC, LoadCmd);
165107 break;
166108 #include "llvm/Support/MachO.def"
167109 }
168 auto RemainingBytes = LoadCmd.C.cmdsize - (EndPtr - LoadCmd.Ptr);
169 if (!std::all_of(EndPtr, &EndPtr[RemainingBytes],
170 [](const char C) { return C == 0; })) {
171 LC.PayloadBytes.insert(LC.PayloadBytes.end(), EndPtr,
172 &EndPtr[RemainingBytes]);
173 RemainingBytes = 0;
110 switch (LoadCmd.C.cmd) {
111 case MachO::LC_SEGMENT:
112 extractSections(
113 LoadCmd, LC.Sections, Obj.isLittleEndian());
114 break;
115 case MachO::LC_SEGMENT_64:
116 extractSections(
117 LoadCmd, LC.Sections, Obj.isLittleEndian());
118 break;
174119 }
175 LC.ZeroPadBytes = RemainingBytes;
176120 Y->LoadCommands.push_back(std::move(LC));
177121 }
178122
9393 return TempSec;
9494 }
9595
96 template
97 size_t writeLoadCommandData(MachOYAML::LoadCommand &LC, raw_ostream &OS) {
98 return 0;
99 }
100
101 template <>
102 size_t writeLoadCommandData(MachOYAML::LoadCommand &LC,
103 raw_ostream &OS) {
104 size_t BytesWritten = 0;
105 for (auto Sec : LC.Sections) {
106 auto TempSec = constructSection(Sec);
107 OS.write(reinterpret_cast(&(TempSec)),
108 sizeof(MachO::section));
109 BytesWritten += sizeof(MachO::section);
110 }
111 return BytesWritten;
112 }
113
114 template <>
115 size_t
116 writeLoadCommandData(MachOYAML::LoadCommand &LC,
117 raw_ostream &OS) {
118 size_t BytesWritten = 0;
119 for (auto Sec : LC.Sections) {
120 auto TempSec = constructSection(Sec);
121 TempSec.reserved3 = Sec.reserved3;
122 OS.write(reinterpret_cast(&(TempSec)),
123 sizeof(MachO::section_64));
124 BytesWritten += sizeof(MachO::section_64);
125 }
126 return BytesWritten;
127 }
128
129 size_t writePayloadString(MachOYAML::LoadCommand &LC, raw_ostream &OS) {
130 size_t BytesWritten = 0;
131 if (!LC.PayloadString.empty()) {
132 OS.write(LC.PayloadString.c_str(), LC.PayloadString.length());
133 BytesWritten = LC.PayloadString.length();
134 }
135 return BytesWritten;
136 }
137
138 template <>
139 size_t writeLoadCommandData(MachOYAML::LoadCommand &LC,
140 raw_ostream &OS) {
141 return writePayloadString(LC, OS);
142 }
143
144 template <>
145 size_t writeLoadCommandData(MachOYAML::LoadCommand &LC,
146 raw_ostream &OS) {
147 return writePayloadString(LC, OS);
148 }
149
15096 Error MachOWriter::writeLoadCommands(raw_ostream &OS) {
15197 for (auto &LC : Obj.LoadCommands) {
15298 size_t BytesWritten = 0;
155101 OS.write(reinterpret_cast(&(LC.Data.LCStruct##_data)), \
156102 sizeof(MachO::LCStruct)); \
157103 BytesWritten = sizeof(MachO::LCStruct); \
158 BytesWritten += writeLoadCommandData(LC, OS); \
159104 break;
160105
161106 switch (LC.Data.load_command_data.cmd) {
163108 OS.write(reinterpret_cast(&(LC.Data.load_command_data)),
164109 sizeof(MachO::load_command));
165110 BytesWritten = sizeof(MachO::load_command);
166 BytesWritten += writeLoadCommandData(LC, OS);
167111 break;
168112 #include "llvm/Support/MachO.def"
169113 }
170114
171 if (LC.PayloadBytes.size() > 0) {
172 OS.write(reinterpret_cast(LC.PayloadBytes.data()),
173 LC.PayloadBytes.size());
174 BytesWritten += LC.PayloadBytes.size();
115 if(LC.Data.load_command_data.cmd == MachO::LC_SEGMENT) {
116 for(auto Sec : LC.Sections) {
117 auto TempSec = constructSection(Sec);
118 OS.write(reinterpret_cast(&(TempSec)), sizeof(MachO::section));
119 BytesWritten += sizeof(MachO::section);
120 }
121 } else if(LC.Data.load_command_data.cmd == MachO::LC_SEGMENT_64) {
122 for(auto Sec : LC.Sections) {
123 auto TempSec = constructSection(Sec);
124 TempSec.reserved3 = Sec.reserved3;
125 OS.write(reinterpret_cast(&(TempSec)), sizeof(MachO::section_64));
126 BytesWritten += sizeof(MachO::section_64);
127 }
175128 }
176129
177 if (LC.ZeroPadBytes > 0) {
178 std::vector FillData;
179 FillData.insert(FillData.begin(), LC.ZeroPadBytes, 0);
180 OS.write(reinterpret_cast(FillData.data()), LC.ZeroPadBytes);
181 BytesWritten += LC.ZeroPadBytes;
182 }
183
184 auto BytesRemaining = LC.Data.load_command_data.cmdsize - BytesWritten;
130 auto BytesRemaining =
131 LC.Data.load_command_data.cmdsize - BytesWritten;
185132 if (BytesRemaining > 0) {
186133 // TODO: Replace all this once the load command data is present in yaml.
187134 // For now I fill with 0xDEADBEEF because it is easy to spot on a hex