llvm.org GIT mirror llvm / f16c2bb
Don't fetch pointers from a InMemoryStruct. InMemoryStruct is extremely dangerous as it returns data from an internal buffer when the endiannes doesn't match. This should fix the tests on big endian hosts. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@178875 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 7 years ago
7 changed file(s) with 48 addition(s) and 44 deletion(s). Raw diff Collapse all Expand all
1414 #ifndef LLVM_OBJECT_MACHO_H
1515 #define LLVM_OBJECT_MACHO_H
1616
17 #include "llvm/ADT/ArrayRef.h"
1718 #include "llvm/ADT/SmallVector.h"
1819 #include "llvm/Object/MachOObject.h"
1920 #include "llvm/Object/ObjectFile.h"
4647 // In a MachO file, sections have a segment name. This is used in the .o
4748 // files. They have a single segment, but this field specifies which segment
4849 // a section should be put in in the final object.
49 error_code getSectionFinalSegmentName(DataRefImpl Sec, StringRef &Res) const;
50 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
51
52 // Names are stored as 16 bytes. These returns the raw 16 bytes without
53 // interpreting them as a C string.
54 ArrayRef getSectionRawName(DataRefImpl Sec) const;
55 ArrayRefgetSectionRawFinalSegmentName(DataRefImpl Sec) const;
5056
5157 MachOObject *getObject() { return MachOObj.get(); }
5258
480480 return StringRef(P, 16);
481481 }
482482
483 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
484 StringRef &Result) const {
483 ArrayRef MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
485484 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
486485 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
487486 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
489488 StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section64));
490489 const macho::Section64 *sec =
491490 reinterpret_cast(Data.data());
492 Result = parseSegmentOrSectionName(sec->Name);
491 return ArrayRef(sec->Name, 16);
493492 } else {
494493 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
495494 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
497496 StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section));
498497 const macho::Section *sec =
499498 reinterpret_cast(Data.data());
500 Result = parseSegmentOrSectionName(sec->Name);
501 }
502 return object_error::success;
503 }
504
505 error_code MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec,
506 StringRef &Res) const {
499 return ArrayRef(sec->Name, 16);
500 }
501 }
502
503 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
504 StringRef &Result) const {
505 ArrayRef Raw = getSectionRawName(DRI);
506 Result = parseSegmentOrSectionName(Raw.data());
507 return object_error::success;
508 }
509
510 ArrayRef
511 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
507512 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
508513 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
509514 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
511516 StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section64));
512517 const macho::Section64 *sec =
513518 reinterpret_cast(Data.data());
514 Res = parseSegmentOrSectionName(sec->SegmentName);
519 return ArrayRef(sec->SegmentName, 16);
515520 } else {
516521 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
517522 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
519524 StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section));
520525 const macho::Section *sec =
521526 reinterpret_cast(Data.data());
522 Res = parseSegmentOrSectionName(sec->SegmentName);
523 }
524 return object_error::success;
527 return ArrayRef(sec->SegmentName);
528 }
529 }
530
531 StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
532 ArrayRef Raw = getSectionRawFinalSegmentName(DRI);
533 return parseSegmentOrSectionName(Raw.data());
525534 }
526535
527536 error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
134134 MACHO: Sections [
135135 MACHO-NEXT: Section {
136136 MACHO-NEXT: Index: 0
137 MACHO-NEXT: Name: __text (5F 5F 74 65 78 74 00
138 MACHO-NEXT: Segment:
137 MACHO-NEXT: Name: __text (5F 5F 74 65 78 74 00 00 00 00 00 00 00 00 00 00)
138 MACHO-NEXT: Segment: __TEXT (5F 5F 54 45 58 54 00 00 00 00 00 00 00 00 00 00)
139139 MACHO-NEXT: Address: 0x0
140140 MACHO-NEXT: Size: 0x22
141141 MACHO-NEXT: Offset: 324
7878 MACHO: Sections [
7979 MACHO-NEXT: Section {
8080 MACHO-NEXT: Index: 0
81 MACHO-NEXT: Name: __text (
82 MACHO-NEXT: Segment:
81 MACHO-NEXT: Name: __text (5F 5F 74 65 78 74 00 00 00 00 00 00 00 00 00 00)
82 MACHO-NEXT: Segment: __TEXT (5F 5F 54 45 58 54 00 00 00 00 00 00 00 00 00 00)
8383 MACHO-NEXT: Address: 0x0
8484 MACHO-NEXT: Size: 0x22
8585 MACHO-NEXT: Offset: 324
9696 MACHO-NEXT: }
9797 MACHO-NEXT: Section {
9898 MACHO-NEXT: Index: 1
99 MACHO-NEXT: Name: __cstring (
100 MACHO-NEXT: Segment:
99 MACHO-NEXT: Name: __cstring (5F 5F 63 73 74 72 69 6E 67 00 00 00 00 00 00 00)
100 MACHO-NEXT: Segment: __TEXT (5F 5F 54 45 58 54 00 00 00 00 00 00 00 00 00 00)
101101 MACHO-NEXT: Address: 0x22
102102 MACHO-NEXT: Size: 0xD
103103 MACHO-NEXT: Offset: 358
336336 SectName != "__text")
337337 continue; // Skip non-text sections
338338
339 StringRef SegmentName;
340339 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
341 if (MachOOF->getSectionFinalSegmentName(DR, SegmentName) ||
342 SegmentName != "__TEXT")
340 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
341 if (SegmentName != "__TEXT")
343342 continue;
344343
345344 // Insert the functions from the function starts segment into our map.
256256 StringRef SegmentName = "";
257257 if (const MachOObjectFile *MachO = dyn_cast(Obj)) {
258258 DataRefImpl DR = i->getRawDataRefImpl();
259 if (error(MachO->getSectionFinalSegmentName(DR, SegmentName)))
260 break;
259 SegmentName = MachO->getSectionFinalSegmentName(DR);
261260 }
262261 StringRef name;
263262 if (error(i->getName(name))) break;
592591 outs() << "*UND*";
593592 else {
594593 if (const MachOObjectFile *MachO = dyn_cast(o)) {
595 StringRef SegmentName;
596594 DataRefImpl DR = Section->getRawDataRefImpl();
597 if (error(MachO->getSectionFinalSegmentName(DR, SegmentName)))
598 SegmentName = "";
595 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
599596 outs() << SegmentName << ",";
600597 }
601598 StringRef SectionName;
156156 };
157157 }
158158
159 static StringRef parseSegmentOrSectionName(ArrayRef P) {
160 if (P[15] == 0)
161 // Null terminated.
162 return StringRef(P.data());
163 // Not null terminated, so this is a 16 char string.
164 return StringRef(P.data(), 16);
165 }
166
167159 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
168160 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
169161 if (LCI.Command.Type == macho::LCT_Segment64)
180172 InMemoryStruct Sect;
181173 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
182174
183 Section.Name = ArrayRef(Sect->Name);
184 Section.SegmentName = ArrayRef(Sect->SegmentName);
185175 Section.Address = Sect->Address;
186176 Section.Size = Sect->Size;
187177 Section.Offset = Sect->Offset;
195185 InMemoryStruct Sect;
196186 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
197187
198 Section.Name = Sect->Name;
199 Section.SegmentName = Sect->SegmentName;
200188 Section.Address = Sect->Address;
201189 Section.Size = Sect->Size;
202190 Section.Offset = Sect->Offset;
269257
270258 MachOSection Section;
271259 getSection(MachO, SecI->getRawDataRefImpl(), Section);
260 DataRefImpl DR = SecI->getRawDataRefImpl();
261
272262 StringRef Name;
273263 if (error(SecI->getName(Name)))
274264 Name = "";
275265
266 ArrayRef RawName = Obj->getSectionRawName(DR);
267 StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
268 ArrayRef RawSegmentName = Obj->getSectionRawFinalSegmentName(DR);
269
276270 DictScope SectionD(W, "Section");
277271 W.printNumber("Index", SectionIndex);
278 W.printBinary("Name", Name, Section.Name);
279 W.printBinary("Segment", parseSegmentOrSectionName(Section.SegmentName),
280 Section.SegmentName);
272 W.printBinary("Name", Name, RawName);
273 W.printBinary("Segment", SegmentName, RawSegmentName);
281274 W.printHex ("Address", Section.Address);
282275 W.printHex ("Size", Section.Size);
283276 W.printNumber("Offset", Section.Offset);