mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-06-20 10:24:12 +00:00
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
This commit is contained in:
@ -15,6 +15,7 @@
|
|||||||
#ifndef LLVM_OBJECT_MACHO_H
|
#ifndef LLVM_OBJECT_MACHO_H
|
||||||
#define LLVM_OBJECT_MACHO_H
|
#define LLVM_OBJECT_MACHO_H
|
||||||
|
|
||||||
|
#include "llvm/ADT/ArrayRef.h"
|
||||||
#include "llvm/ADT/SmallVector.h"
|
#include "llvm/ADT/SmallVector.h"
|
||||||
#include "llvm/Object/MachOObject.h"
|
#include "llvm/Object/MachOObject.h"
|
||||||
#include "llvm/Object/ObjectFile.h"
|
#include "llvm/Object/ObjectFile.h"
|
||||||
@ -47,7 +48,12 @@ public:
|
|||||||
// In a MachO file, sections have a segment name. This is used in the .o
|
// In a MachO file, sections have a segment name. This is used in the .o
|
||||||
// files. They have a single segment, but this field specifies which segment
|
// files. They have a single segment, but this field specifies which segment
|
||||||
// a section should be put in in the final object.
|
// a section should be put in in the final object.
|
||||||
error_code getSectionFinalSegmentName(DataRefImpl Sec, StringRef &Res) const;
|
StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
|
||||||
|
|
||||||
|
// Names are stored as 16 bytes. These returns the raw 16 bytes without
|
||||||
|
// interpreting them as a C string.
|
||||||
|
ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
|
||||||
|
ArrayRef<char>getSectionRawFinalSegmentName(DataRefImpl Sec) const;
|
||||||
|
|
||||||
MachOObject *getObject() { return MachOObj.get(); }
|
MachOObject *getObject() { return MachOObj.get(); }
|
||||||
|
|
||||||
|
@ -481,8 +481,7 @@ static StringRef parseSegmentOrSectionName(const char *P) {
|
|||||||
return StringRef(P, 16);
|
return StringRef(P, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
|
ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
|
||||||
StringRef &Result) const {
|
|
||||||
if (is64BitLoadCommand(MachOObj.get(), DRI)) {
|
if (is64BitLoadCommand(MachOObj.get(), DRI)) {
|
||||||
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
|
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
|
||||||
unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
|
unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
|
||||||
@ -490,7 +489,7 @@ error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
|
|||||||
StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section64));
|
StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section64));
|
||||||
const macho::Section64 *sec =
|
const macho::Section64 *sec =
|
||||||
reinterpret_cast<const macho::Section64*>(Data.data());
|
reinterpret_cast<const macho::Section64*>(Data.data());
|
||||||
Result = parseSegmentOrSectionName(sec->Name);
|
return ArrayRef<char>(sec->Name, 16);
|
||||||
} else {
|
} else {
|
||||||
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
|
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
|
||||||
unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
|
unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
|
||||||
@ -498,13 +497,19 @@ error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
|
|||||||
StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section));
|
StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section));
|
||||||
const macho::Section *sec =
|
const macho::Section *sec =
|
||||||
reinterpret_cast<const macho::Section*>(Data.data());
|
reinterpret_cast<const macho::Section*>(Data.data());
|
||||||
Result = parseSegmentOrSectionName(sec->Name);
|
return ArrayRef<char>(sec->Name, 16);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
|
||||||
|
StringRef &Result) const {
|
||||||
|
ArrayRef<char> Raw = getSectionRawName(DRI);
|
||||||
|
Result = parseSegmentOrSectionName(Raw.data());
|
||||||
return object_error::success;
|
return object_error::success;
|
||||||
}
|
}
|
||||||
|
|
||||||
error_code MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec,
|
ArrayRef<char>
|
||||||
StringRef &Res) const {
|
MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
|
||||||
if (is64BitLoadCommand(MachOObj.get(), Sec)) {
|
if (is64BitLoadCommand(MachOObj.get(), Sec)) {
|
||||||
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
|
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
|
||||||
unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
|
unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
|
||||||
@ -512,7 +517,7 @@ error_code MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec,
|
|||||||
StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section64));
|
StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section64));
|
||||||
const macho::Section64 *sec =
|
const macho::Section64 *sec =
|
||||||
reinterpret_cast<const macho::Section64*>(Data.data());
|
reinterpret_cast<const macho::Section64*>(Data.data());
|
||||||
Res = parseSegmentOrSectionName(sec->SegmentName);
|
return ArrayRef<char>(sec->SegmentName, 16);
|
||||||
} else {
|
} else {
|
||||||
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
|
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
|
||||||
unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
|
unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
|
||||||
@ -520,9 +525,13 @@ error_code MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec,
|
|||||||
StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section));
|
StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section));
|
||||||
const macho::Section *sec =
|
const macho::Section *sec =
|
||||||
reinterpret_cast<const macho::Section*>(Data.data());
|
reinterpret_cast<const macho::Section*>(Data.data());
|
||||||
Res = parseSegmentOrSectionName(sec->SegmentName);
|
return ArrayRef<char>(sec->SegmentName);
|
||||||
}
|
}
|
||||||
return object_error::success;
|
}
|
||||||
|
|
||||||
|
StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
|
||||||
|
ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
|
||||||
|
return parseSegmentOrSectionName(Raw.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
|
error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
|
||||||
|
@ -135,8 +135,8 @@ ELF-NEXT: }
|
|||||||
MACHO: Sections [
|
MACHO: Sections [
|
||||||
MACHO-NEXT: Section {
|
MACHO-NEXT: Section {
|
||||||
MACHO-NEXT: Index: 0
|
MACHO-NEXT: Index: 0
|
||||||
MACHO-NEXT: Name: __text (5F 5F 74 65 78 74 00
|
MACHO-NEXT: Name: __text (5F 5F 74 65 78 74 00 00 00 00 00 00 00 00 00 00)
|
||||||
MACHO-NEXT: Segment:
|
MACHO-NEXT: Segment: __TEXT (5F 5F 54 45 58 54 00 00 00 00 00 00 00 00 00 00)
|
||||||
MACHO-NEXT: Address: 0x0
|
MACHO-NEXT: Address: 0x0
|
||||||
MACHO-NEXT: Size: 0x22
|
MACHO-NEXT: Size: 0x22
|
||||||
MACHO-NEXT: Offset: 324
|
MACHO-NEXT: Offset: 324
|
||||||
|
@ -79,8 +79,8 @@ ELF-NEXT: }
|
|||||||
MACHO: Sections [
|
MACHO: Sections [
|
||||||
MACHO-NEXT: Section {
|
MACHO-NEXT: Section {
|
||||||
MACHO-NEXT: Index: 0
|
MACHO-NEXT: Index: 0
|
||||||
MACHO-NEXT: Name: __text (
|
MACHO-NEXT: Name: __text (5F 5F 74 65 78 74 00 00 00 00 00 00 00 00 00 00)
|
||||||
MACHO-NEXT: Segment:
|
MACHO-NEXT: Segment: __TEXT (5F 5F 54 45 58 54 00 00 00 00 00 00 00 00 00 00)
|
||||||
MACHO-NEXT: Address: 0x0
|
MACHO-NEXT: Address: 0x0
|
||||||
MACHO-NEXT: Size: 0x22
|
MACHO-NEXT: Size: 0x22
|
||||||
MACHO-NEXT: Offset: 324
|
MACHO-NEXT: Offset: 324
|
||||||
@ -97,8 +97,8 @@ MACHO-NEXT: Reserved2: 0x0
|
|||||||
MACHO-NEXT: }
|
MACHO-NEXT: }
|
||||||
MACHO-NEXT: Section {
|
MACHO-NEXT: Section {
|
||||||
MACHO-NEXT: Index: 1
|
MACHO-NEXT: Index: 1
|
||||||
MACHO-NEXT: Name: __cstring (
|
MACHO-NEXT: Name: __cstring (5F 5F 63 73 74 72 69 6E 67 00 00 00 00 00 00 00)
|
||||||
MACHO-NEXT: Segment:
|
MACHO-NEXT: Segment: __TEXT (5F 5F 54 45 58 54 00 00 00 00 00 00 00 00 00 00)
|
||||||
MACHO-NEXT: Address: 0x22
|
MACHO-NEXT: Address: 0x22
|
||||||
MACHO-NEXT: Size: 0xD
|
MACHO-NEXT: Size: 0xD
|
||||||
MACHO-NEXT: Offset: 358
|
MACHO-NEXT: Offset: 358
|
||||||
|
@ -337,10 +337,9 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
|
|||||||
SectName != "__text")
|
SectName != "__text")
|
||||||
continue; // Skip non-text sections
|
continue; // Skip non-text sections
|
||||||
|
|
||||||
StringRef SegmentName;
|
|
||||||
DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
|
DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
|
||||||
if (MachOOF->getSectionFinalSegmentName(DR, SegmentName) ||
|
StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
|
||||||
SegmentName != "__TEXT")
|
if (SegmentName != "__TEXT")
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Insert the functions from the function starts segment into our map.
|
// Insert the functions from the function starts segment into our map.
|
||||||
|
@ -257,8 +257,7 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
|
|||||||
StringRef SegmentName = "";
|
StringRef SegmentName = "";
|
||||||
if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
|
if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
|
||||||
DataRefImpl DR = i->getRawDataRefImpl();
|
DataRefImpl DR = i->getRawDataRefImpl();
|
||||||
if (error(MachO->getSectionFinalSegmentName(DR, SegmentName)))
|
SegmentName = MachO->getSectionFinalSegmentName(DR);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
StringRef name;
|
StringRef name;
|
||||||
if (error(i->getName(name))) break;
|
if (error(i->getName(name))) break;
|
||||||
@ -593,10 +592,8 @@ static void PrintSymbolTable(const ObjectFile *o) {
|
|||||||
outs() << "*UND*";
|
outs() << "*UND*";
|
||||||
else {
|
else {
|
||||||
if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(o)) {
|
if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(o)) {
|
||||||
StringRef SegmentName;
|
|
||||||
DataRefImpl DR = Section->getRawDataRefImpl();
|
DataRefImpl DR = Section->getRawDataRefImpl();
|
||||||
if (error(MachO->getSectionFinalSegmentName(DR, SegmentName)))
|
StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
|
||||||
SegmentName = "";
|
|
||||||
outs() << SegmentName << ",";
|
outs() << SegmentName << ",";
|
||||||
}
|
}
|
||||||
StringRef SectionName;
|
StringRef SectionName;
|
||||||
|
@ -157,14 +157,6 @@ namespace {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static StringRef parseSegmentOrSectionName(ArrayRef<char> P) {
|
|
||||||
if (P[15] == 0)
|
|
||||||
// Null terminated.
|
|
||||||
return StringRef(P.data());
|
|
||||||
// Not null terminated, so this is a 16 char string.
|
|
||||||
return StringRef(P.data(), 16);
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
|
static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
|
||||||
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
|
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
|
||||||
if (LCI.Command.Type == macho::LCT_Segment64)
|
if (LCI.Command.Type == macho::LCT_Segment64)
|
||||||
@ -181,8 +173,6 @@ static void getSection(const MachOObject *MachOObj,
|
|||||||
InMemoryStruct<macho::Section64> Sect;
|
InMemoryStruct<macho::Section64> Sect;
|
||||||
MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
|
MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
|
||||||
|
|
||||||
Section.Name = ArrayRef<char>(Sect->Name);
|
|
||||||
Section.SegmentName = ArrayRef<char>(Sect->SegmentName);
|
|
||||||
Section.Address = Sect->Address;
|
Section.Address = Sect->Address;
|
||||||
Section.Size = Sect->Size;
|
Section.Size = Sect->Size;
|
||||||
Section.Offset = Sect->Offset;
|
Section.Offset = Sect->Offset;
|
||||||
@ -196,8 +186,6 @@ static void getSection(const MachOObject *MachOObj,
|
|||||||
InMemoryStruct<macho::Section> Sect;
|
InMemoryStruct<macho::Section> Sect;
|
||||||
MachOObj->ReadSection(LCI, DRI.d.b, Sect);
|
MachOObj->ReadSection(LCI, DRI.d.b, Sect);
|
||||||
|
|
||||||
Section.Name = Sect->Name;
|
|
||||||
Section.SegmentName = Sect->SegmentName;
|
|
||||||
Section.Address = Sect->Address;
|
Section.Address = Sect->Address;
|
||||||
Section.Size = Sect->Size;
|
Section.Size = Sect->Size;
|
||||||
Section.Offset = Sect->Offset;
|
Section.Offset = Sect->Offset;
|
||||||
@ -270,15 +258,20 @@ void MachODumper::printSections() {
|
|||||||
|
|
||||||
MachOSection Section;
|
MachOSection Section;
|
||||||
getSection(MachO, SecI->getRawDataRefImpl(), Section);
|
getSection(MachO, SecI->getRawDataRefImpl(), Section);
|
||||||
|
DataRefImpl DR = SecI->getRawDataRefImpl();
|
||||||
|
|
||||||
StringRef Name;
|
StringRef Name;
|
||||||
if (error(SecI->getName(Name)))
|
if (error(SecI->getName(Name)))
|
||||||
Name = "";
|
Name = "";
|
||||||
|
|
||||||
|
ArrayRef<char> RawName = Obj->getSectionRawName(DR);
|
||||||
|
StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
|
||||||
|
ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR);
|
||||||
|
|
||||||
DictScope SectionD(W, "Section");
|
DictScope SectionD(W, "Section");
|
||||||
W.printNumber("Index", SectionIndex);
|
W.printNumber("Index", SectionIndex);
|
||||||
W.printBinary("Name", Name, Section.Name);
|
W.printBinary("Name", Name, RawName);
|
||||||
W.printBinary("Segment", parseSegmentOrSectionName(Section.SegmentName),
|
W.printBinary("Segment", SegmentName, RawSegmentName);
|
||||||
Section.SegmentName);
|
|
||||||
W.printHex ("Address", Section.Address);
|
W.printHex ("Address", Section.Address);
|
||||||
W.printHex ("Size", Section.Size);
|
W.printHex ("Size", Section.Size);
|
||||||
W.printNumber("Offset", Section.Offset);
|
W.printNumber("Offset", Section.Offset);
|
||||||
|
Reference in New Issue
Block a user