llvm.org GIT mirror llvm / 11c2058
extracting swapStruct into include/llvm/Support/MachO.h (no functional change) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@213361 91177308-0d34-0410-b5e6-96231b3b80d8 Artyom Skrobov 5 years ago
2 changed file(s) with 216 addition(s) and 208 deletion(s). Raw diff Collapse all Expand all
958958 };
959959
960960 // Structs from
961 struct nlist_base {
962 uint32_t n_strx;
963 uint8_t n_type;
964 uint8_t n_sect;
965 uint16_t n_desc;
966 };
967
961968 struct nlist {
962969 uint32_t n_strx;
963970 uint8_t n_type;
973980 uint16_t n_desc;
974981 uint64_t n_value;
975982 };
983
984
985 // Byte order swapping functions for MachO structs
986
987 inline void swapStruct(mach_header &mh) {
988 sys::swapByteOrder(mh.magic);
989 sys::swapByteOrder(mh.cputype);
990 sys::swapByteOrder(mh.cpusubtype);
991 sys::swapByteOrder(mh.filetype);
992 sys::swapByteOrder(mh.ncmds);
993 sys::swapByteOrder(mh.sizeofcmds);
994 sys::swapByteOrder(mh.flags);
995 }
996
997 inline void swapStruct(mach_header_64 &H) {
998 sys::swapByteOrder(H.magic);
999 sys::swapByteOrder(H.cputype);
1000 sys::swapByteOrder(H.cpusubtype);
1001 sys::swapByteOrder(H.filetype);
1002 sys::swapByteOrder(H.ncmds);
1003 sys::swapByteOrder(H.sizeofcmds);
1004 sys::swapByteOrder(H.flags);
1005 sys::swapByteOrder(H.reserved);
1006 }
1007
1008 inline void swapStruct(load_command &lc) {
1009 sys::swapByteOrder(lc.cmd);
1010 sys::swapByteOrder(lc.cmdsize);
1011 }
1012
1013 inline void swapStruct(symtab_command &lc) {
1014 sys::swapByteOrder(lc.cmd);
1015 sys::swapByteOrder(lc.cmdsize);
1016 sys::swapByteOrder(lc.symoff);
1017 sys::swapByteOrder(lc.nsyms);
1018 sys::swapByteOrder(lc.stroff);
1019 sys::swapByteOrder(lc.strsize);
1020 }
1021
1022 inline void swapStruct(segment_command_64 &seg) {
1023 sys::swapByteOrder(seg.cmd);
1024 sys::swapByteOrder(seg.cmdsize);
1025 sys::swapByteOrder(seg.vmaddr);
1026 sys::swapByteOrder(seg.vmsize);
1027 sys::swapByteOrder(seg.fileoff);
1028 sys::swapByteOrder(seg.filesize);
1029 sys::swapByteOrder(seg.maxprot);
1030 sys::swapByteOrder(seg.initprot);
1031 sys::swapByteOrder(seg.nsects);
1032 sys::swapByteOrder(seg.flags);
1033 }
1034
1035 inline void swapStruct(segment_command &seg) {
1036 sys::swapByteOrder(seg.cmd);
1037 sys::swapByteOrder(seg.cmdsize);
1038 sys::swapByteOrder(seg.vmaddr);
1039 sys::swapByteOrder(seg.vmsize);
1040 sys::swapByteOrder(seg.fileoff);
1041 sys::swapByteOrder(seg.filesize);
1042 sys::swapByteOrder(seg.maxprot);
1043 sys::swapByteOrder(seg.initprot);
1044 sys::swapByteOrder(seg.nsects);
1045 sys::swapByteOrder(seg.flags);
1046 }
1047
1048 inline void swapStruct(section_64 §) {
1049 sys::swapByteOrder(sect.addr);
1050 sys::swapByteOrder(sect.size);
1051 sys::swapByteOrder(sect.offset);
1052 sys::swapByteOrder(sect.align);
1053 sys::swapByteOrder(sect.reloff);
1054 sys::swapByteOrder(sect.nreloc);
1055 sys::swapByteOrder(sect.flags);
1056 sys::swapByteOrder(sect.reserved1);
1057 sys::swapByteOrder(sect.reserved2);
1058 }
1059
1060 inline void swapStruct(section §) {
1061 sys::swapByteOrder(sect.addr);
1062 sys::swapByteOrder(sect.size);
1063 sys::swapByteOrder(sect.offset);
1064 sys::swapByteOrder(sect.align);
1065 sys::swapByteOrder(sect.reloff);
1066 sys::swapByteOrder(sect.nreloc);
1067 sys::swapByteOrder(sect.flags);
1068 sys::swapByteOrder(sect.reserved1);
1069 sys::swapByteOrder(sect.reserved2);
1070 }
1071
1072 inline void swapStruct(dyld_info_command &info) {
1073 sys::swapByteOrder(info.cmd);
1074 sys::swapByteOrder(info.cmdsize);
1075 sys::swapByteOrder(info.rebase_off);
1076 sys::swapByteOrder(info.rebase_size);
1077 sys::swapByteOrder(info.bind_off);
1078 sys::swapByteOrder(info.bind_size);
1079 sys::swapByteOrder(info.weak_bind_off);
1080 sys::swapByteOrder(info.weak_bind_size);
1081 sys::swapByteOrder(info.lazy_bind_off);
1082 sys::swapByteOrder(info.lazy_bind_size);
1083 sys::swapByteOrder(info.export_off);
1084 sys::swapByteOrder(info.export_size);
1085 }
1086
1087 inline void swapStruct(dylib_command &d) {
1088 sys::swapByteOrder(d.cmd);
1089 sys::swapByteOrder(d.cmdsize);
1090 sys::swapByteOrder(d.dylib.name);
1091 sys::swapByteOrder(d.dylib.timestamp);
1092 sys::swapByteOrder(d.dylib.current_version);
1093 sys::swapByteOrder(d.dylib.compatibility_version);
1094 }
1095
1096 inline void swapStruct(dylinker_command &d) {
1097 sys::swapByteOrder(d.cmd);
1098 sys::swapByteOrder(d.cmdsize);
1099 sys::swapByteOrder(d.name);
1100 }
1101
1102 inline void swapStruct(entry_point_command &e) {
1103 sys::swapByteOrder(e.cmd);
1104 sys::swapByteOrder(e.cmdsize);
1105 sys::swapByteOrder(e.entryoff);
1106 sys::swapByteOrder(e.stacksize);
1107 }
1108
1109 inline void swapStruct(dysymtab_command &dst) {
1110 sys::swapByteOrder(dst.cmd);
1111 sys::swapByteOrder(dst.cmdsize);
1112 sys::swapByteOrder(dst.ilocalsym);
1113 sys::swapByteOrder(dst.nlocalsym);
1114 sys::swapByteOrder(dst.iextdefsym);
1115 sys::swapByteOrder(dst.nextdefsym);
1116 sys::swapByteOrder(dst.iundefsym);
1117 sys::swapByteOrder(dst.nundefsym);
1118 sys::swapByteOrder(dst.tocoff);
1119 sys::swapByteOrder(dst.ntoc);
1120 sys::swapByteOrder(dst.modtaboff);
1121 sys::swapByteOrder(dst.nmodtab);
1122 sys::swapByteOrder(dst.extrefsymoff);
1123 sys::swapByteOrder(dst.nextrefsyms);
1124 sys::swapByteOrder(dst.indirectsymoff);
1125 sys::swapByteOrder(dst.nindirectsyms);
1126 sys::swapByteOrder(dst.extreloff);
1127 sys::swapByteOrder(dst.nextrel);
1128 sys::swapByteOrder(dst.locreloff);
1129 sys::swapByteOrder(dst.nlocrel);
1130 }
1131
1132 inline void swapStruct(any_relocation_info &reloc) {
1133 sys::swapByteOrder(reloc.r_word0);
1134 sys::swapByteOrder(reloc.r_word1);
1135 }
1136
1137 inline void swapStruct(nlist_base &S) {
1138 sys::swapByteOrder(S.n_strx);
1139 sys::swapByteOrder(S.n_desc);
1140 }
1141
1142 inline void swapStruct(nlist &sym) {
1143 sys::swapByteOrder(sym.n_strx);
1144 sys::swapByteOrder(sym.n_desc);
1145 sys::swapByteOrder(sym.n_value);
1146 }
1147
1148 inline void swapStruct(nlist_64 &sym) {
1149 sys::swapByteOrder(sym.n_strx);
1150 sys::swapByteOrder(sym.n_desc);
1151 sys::swapByteOrder(sym.n_value);
1152 }
1153
1154 inline void swapStruct(linkedit_data_command &C) {
1155 sys::swapByteOrder(C.cmd);
1156 sys::swapByteOrder(C.cmdsize);
1157 sys::swapByteOrder(C.dataoff);
1158 sys::swapByteOrder(C.datasize);
1159 }
1160
1161 inline void swapStruct(linker_options_command &C) {
1162 sys::swapByteOrder(C.cmd);
1163 sys::swapByteOrder(C.cmdsize);
1164 sys::swapByteOrder(C.count);
1165 }
1166
1167 inline void swapStruct(version_min_command&C) {
1168 sys::swapByteOrder(C.cmd);
1169 sys::swapByteOrder(C.cmdsize);
1170 sys::swapByteOrder(C.version);
1171 sys::swapByteOrder(C.reserved);
1172 }
1173
1174 inline void swapStruct(data_in_code_entry &C) {
1175 sys::swapByteOrder(C.offset);
1176 sys::swapByteOrder(C.length);
1177 sys::swapByteOrder(C.kind);
1178 }
1179
1180 inline void swapStruct(uint32_t &C) {
1181 sys::swapByteOrder(C);
1182 }
9761183
9771184 // Get/Set functions from
9781185
3030
3131 namespace object {
3232
33 struct nlist_base {
34 uint32_t n_strx;
35 uint8_t n_type;
36 uint8_t n_sect;
37 uint16_t n_desc;
38 };
39
4033 struct section_base {
4134 char sectname[16];
4235 char segname[16];
4336 };
44
45 template
46 static void SwapStruct(T &Value);
47
48 template<>
49 void SwapStruct(MachO::any_relocation_info &H) {
50 sys::swapByteOrder(H.r_word0);
51 sys::swapByteOrder(H.r_word1);
52 }
53
54 template<>
55 void SwapStruct(MachO::load_command &L) {
56 sys::swapByteOrder(L.cmd);
57 sys::swapByteOrder(L.cmdsize);
58 }
59
60 template<>
61 void SwapStruct(nlist_base &S) {
62 sys::swapByteOrder(S.n_strx);
63 sys::swapByteOrder(S.n_desc);
64 }
65
66 template<>
67 void SwapStruct(MachO::section &S) {
68 sys::swapByteOrder(S.addr);
69 sys::swapByteOrder(S.size);
70 sys::swapByteOrder(S.offset);
71 sys::swapByteOrder(S.align);
72 sys::swapByteOrder(S.reloff);
73 sys::swapByteOrder(S.nreloc);
74 sys::swapByteOrder(S.flags);
75 sys::swapByteOrder(S.reserved1);
76 sys::swapByteOrder(S.reserved2);
77 }
78
79 template<>
80 void SwapStruct(MachO::section_64 &S) {
81 sys::swapByteOrder(S.addr);
82 sys::swapByteOrder(S.size);
83 sys::swapByteOrder(S.offset);
84 sys::swapByteOrder(S.align);
85 sys::swapByteOrder(S.reloff);
86 sys::swapByteOrder(S.nreloc);
87 sys::swapByteOrder(S.flags);
88 sys::swapByteOrder(S.reserved1);
89 sys::swapByteOrder(S.reserved2);
90 sys::swapByteOrder(S.reserved3);
91 }
92
93 template<>
94 void SwapStruct(MachO::nlist &S) {
95 sys::swapByteOrder(S.n_strx);
96 sys::swapByteOrder(S.n_desc);
97 sys::swapByteOrder(S.n_value);
98 }
99
100 template<>
101 void SwapStruct(MachO::nlist_64 &S) {
102 sys::swapByteOrder(S.n_strx);
103 sys::swapByteOrder(S.n_desc);
104 sys::swapByteOrder(S.n_value);
105 }
106
107 template<>
108 void SwapStruct(MachO::mach_header &H) {
109 sys::swapByteOrder(H.magic);
110 sys::swapByteOrder(H.cputype);
111 sys::swapByteOrder(H.cpusubtype);
112 sys::swapByteOrder(H.filetype);
113 sys::swapByteOrder(H.ncmds);
114 sys::swapByteOrder(H.sizeofcmds);
115 sys::swapByteOrder(H.flags);
116 }
117
118 template<>
119 void SwapStruct(MachO::mach_header_64 &H) {
120 sys::swapByteOrder(H.magic);
121 sys::swapByteOrder(H.cputype);
122 sys::swapByteOrder(H.cpusubtype);
123 sys::swapByteOrder(H.filetype);
124 sys::swapByteOrder(H.ncmds);
125 sys::swapByteOrder(H.sizeofcmds);
126 sys::swapByteOrder(H.flags);
127 sys::swapByteOrder(H.reserved);
128 }
129
130 template<>
131 void SwapStruct(MachO::symtab_command &C) {
132 sys::swapByteOrder(C.cmd);
133 sys::swapByteOrder(C.cmdsize);
134 sys::swapByteOrder(C.symoff);
135 sys::swapByteOrder(C.nsyms);
136 sys::swapByteOrder(C.stroff);
137 sys::swapByteOrder(C.strsize);
138 }
139
140 template<>
141 void SwapStruct(MachO::dysymtab_command &C) {
142 sys::swapByteOrder(C.cmd);
143 sys::swapByteOrder(C.cmdsize);
144 sys::swapByteOrder(C.ilocalsym);
145 sys::swapByteOrder(C.nlocalsym);
146 sys::swapByteOrder(C.iextdefsym);
147 sys::swapByteOrder(C.nextdefsym);
148 sys::swapByteOrder(C.iundefsym);
149 sys::swapByteOrder(C.nundefsym);
150 sys::swapByteOrder(C.tocoff);
151 sys::swapByteOrder(C.ntoc);
152 sys::swapByteOrder(C.modtaboff);
153 sys::swapByteOrder(C.nmodtab);
154 sys::swapByteOrder(C.extrefsymoff);
155 sys::swapByteOrder(C.nextrefsyms);
156 sys::swapByteOrder(C.indirectsymoff);
157 sys::swapByteOrder(C.nindirectsyms);
158 sys::swapByteOrder(C.extreloff);
159 sys::swapByteOrder(C.nextrel);
160 sys::swapByteOrder(C.locreloff);
161 sys::swapByteOrder(C.nlocrel);
162 }
163
164 template<>
165 void SwapStruct(MachO::linkedit_data_command &C) {
166 sys::swapByteOrder(C.cmd);
167 sys::swapByteOrder(C.cmdsize);
168 sys::swapByteOrder(C.dataoff);
169 sys::swapByteOrder(C.datasize);
170 }
171
172 template<>
173 void SwapStruct(MachO::segment_command &C) {
174 sys::swapByteOrder(C.cmd);
175 sys::swapByteOrder(C.cmdsize);
176 sys::swapByteOrder(C.vmaddr);
177 sys::swapByteOrder(C.vmsize);
178 sys::swapByteOrder(C.fileoff);
179 sys::swapByteOrder(C.filesize);
180 sys::swapByteOrder(C.maxprot);
181 sys::swapByteOrder(C.initprot);
182 sys::swapByteOrder(C.nsects);
183 sys::swapByteOrder(C.flags);
184 }
185
186 template<>
187 void SwapStruct(MachO::segment_command_64 &C) {
188 sys::swapByteOrder(C.cmd);
189 sys::swapByteOrder(C.cmdsize);
190 sys::swapByteOrder(C.vmaddr);
191 sys::swapByteOrder(C.vmsize);
192 sys::swapByteOrder(C.fileoff);
193 sys::swapByteOrder(C.filesize);
194 sys::swapByteOrder(C.maxprot);
195 sys::swapByteOrder(C.initprot);
196 sys::swapByteOrder(C.nsects);
197 sys::swapByteOrder(C.flags);
198 }
199
200 template<>
201 void SwapStruct(uint32_t &C) {
202 sys::swapByteOrder(C);
203 }
204
205 template<>
206 void SwapStruct(MachO::linker_options_command &C) {
207 sys::swapByteOrder(C.cmd);
208 sys::swapByteOrder(C.cmdsize);
209 sys::swapByteOrder(C.count);
210 }
211
212 template<>
213 void SwapStruct(MachO::version_min_command&C) {
214 sys::swapByteOrder(C.cmd);
215 sys::swapByteOrder(C.cmdsize);
216 sys::swapByteOrder(C.version);
217 sys::swapByteOrder(C.reserved);
218 }
219
220 template<>
221 void SwapStruct(MachO::dylib_command&C) {
222 sys::swapByteOrder(C.cmd);
223 sys::swapByteOrder(C.cmdsize);
224 sys::swapByteOrder(C.dylib.name);
225 sys::swapByteOrder(C.dylib.timestamp);
226 sys::swapByteOrder(C.dylib.current_version);
227 sys::swapByteOrder(C.dylib.compatibility_version);
228 }
229
230 template<>
231 void SwapStruct(MachO::data_in_code_entry &C) {
232 sys::swapByteOrder(C.offset);
233 sys::swapByteOrder(C.length);
234 sys::swapByteOrder(C.kind);
235 }
23637
23738 template
23839 T getStruct(const MachOObjectFile *O, const char *P) {
23940 T Cmd;
24041 memcpy(&Cmd, P, sizeof(T));
24142 if (O->isLittleEndian() != sys::IsLittleEndianHost)
242 SwapStruct(Cmd);
43 MachO::swapStruct(Cmd);
24344 return Cmd;
24445 }
24546
27374 return O->getData().substr(Offset, 1).data();
27475 }
27576
276 static nlist_base
77 static MachO::nlist_base
27778 getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
27879 const char *P = reinterpret_cast(DRI.p);
279 return getStruct<nlist_base>(O, P);
80 return getStruct<MachO::nlist_base>(O, P);
28081 }
28182
28283 static StringRef parseSegmentOrSectionName(const char *P) {
473274 std::error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
474275 StringRef &Res) const {
475276 StringRef StringTable = getStringTableData();
476 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
277 MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
477278 const char *Start = &StringTable.data()[Entry.n_strx];
478279 Res = StringRef(Start);
479280 return object_error::success;
527328 uint32_t &Result) const {
528329 uint32_t flags = getSymbolFlags(DRI);
529330 if (flags & SymbolRef::SF_Common) {
530 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
331 MachO::nlist_base Entry = getSymbolTableEntryBase(this, DRI);
531332 Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
532333 } else {
533334 Result = 0;
541342 uint64_t EndOffset = 0;
542343 uint8_t SectionIndex;
543344
544 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
345 MachO::nlist_base Entry = getSymbolTableEntryBase(this, DRI);
545346 uint64_t Value;
546347 getSymbolAddress(DRI, Value);
547348 if (Value == UnknownAddressOrSize) {
586387
587388 std::error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
588389 SymbolRef::Type &Res) const {
589 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
390 MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
590391 uint8_t n_type = Entry.n_type;
591392
592393 Res = SymbolRef::ST_Other;
609410 }
610411
611412 uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
612 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
413 MachO::nlist_base Entry = getSymbolTableEntryBase(this, DRI);
613414
614415 uint8_t MachOType = Entry.n_type;
615416 uint16_t MachOFlags = Entry.n_desc;
646447
647448 std::error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
648449 section_iterator &Res) const {
649 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
450 MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
650451 uint8_t index = Entry.n_sect;
651452
652453 if (index == 0) {