summaryrefslogtreecommitdiff
path: root/Source/cmMachO.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'Source/cmMachO.cxx')
-rw-r--r--Source/cmMachO.cxx260
1 files changed, 107 insertions, 153 deletions
diff --git a/Source/cmMachO.cxx b/Source/cmMachO.cxx
index 147979d787..314760ee79 100644
--- a/Source/cmMachO.cxx
+++ b/Source/cmMachO.cxx
@@ -43,48 +43,43 @@
namespace {
- // peek in the file
- template <typename T>
- bool peek(cmsys::ifstream& fin, T &v)
- {
- std::streampos p = fin.tellg();
- if(!fin.read(reinterpret_cast<char*>(&v), sizeof(T)))
- {
- return false;
- }
- fin.seekg(p);
- return fin.good();
- }
+// peek in the file
+template <typename T>
+bool peek(cmsys::ifstream& fin, T& v)
+{
+ std::streampos p = fin.tellg();
+ if (!fin.read(reinterpret_cast<char*>(&v), sizeof(T))) {
+ return false;
+ }
+ fin.seekg(p);
+ return fin.good();
+}
- // read from the file and fill a data structure
- template <typename T>
- bool read(cmsys::ifstream& fin, T& v)
- {
- if(!fin.read(reinterpret_cast<char*>(&v), sizeof(T)))
- {
- return false;
- }
- return true;
- }
+// read from the file and fill a data structure
+template <typename T>
+bool read(cmsys::ifstream& fin, T& v)
+{
+ if (!fin.read(reinterpret_cast<char*>(&v), sizeof(T))) {
+ return false;
+ }
+ return true;
+}
- // read from the file and fill multiple data structures where
- // the vector has been resized
- template <typename T>
- bool read(cmsys::ifstream& fin, std::vector<T>& v)
- {
- // nothing to read
- if(v.empty())
- {
- return true;
- }
- if(!fin.read(reinterpret_cast<char*>(&v[0]), sizeof(T) * v.size()))
- {
- return false;
- }
+// read from the file and fill multiple data structures where
+// the vector has been resized
+template <typename T>
+bool read(cmsys::ifstream& fin, std::vector<T>& v)
+{
+ // nothing to read
+ if (v.empty()) {
return true;
- }
+ }
+ if (!fin.read(reinterpret_cast<char*>(&v[0]), sizeof(T) * v.size())) {
+ return false;
+ }
+ return true;
+}
}
-
// Contains header and load commands for a single Mach-O file
class cmMachOHeaderAndLoadCommands
@@ -92,45 +87,41 @@ class cmMachOHeaderAndLoadCommands
public:
// A load_command and its associated data
struct RawLoadCommand
- {
+ {
uint32_t type(const cmMachOHeaderAndLoadCommands* m) const
- {
- if(this->LoadCommand.size() < sizeof(load_command))
- {
+ {
+ if (this->LoadCommand.size() < sizeof(load_command)) {
return 0;
- }
+ }
const load_command* cmd =
reinterpret_cast<const load_command*>(&this->LoadCommand[0]);
return m->swap(cmd->cmd);
- }
+ }
std::vector<char> LoadCommand;
- };
+ };
cmMachOHeaderAndLoadCommands(bool _swap)
: Swap(_swap)
- {
- }
- virtual ~cmMachOHeaderAndLoadCommands()
- {
- }
+ {
+ }
+ virtual ~cmMachOHeaderAndLoadCommands() {}
virtual bool read_mach_o(cmsys::ifstream& fin) = 0;
const std::vector<RawLoadCommand>& load_commands() const
- {
+ {
return this->LoadCommands;
- }
+ }
uint32_t swap(uint32_t v) const
- {
- if(this->Swap)
- {
+ {
+ if (this->Swap) {
char* c = reinterpret_cast<char*>(&v);
std::swap(c[0], c[3]);
std::swap(c[1], c[2]);
- }
- return v;
}
+ return v;
+ }
protected:
bool read_load_commands(uint32_t ncmds, uint32_t sizeofcmds,
@@ -148,14 +139,13 @@ class cmMachOHeaderAndLoadCommandsImpl : public cmMachOHeaderAndLoadCommands
public:
cmMachOHeaderAndLoadCommandsImpl(bool _swap)
: cmMachOHeaderAndLoadCommands(_swap)
- {
- }
+ {
+ }
bool read_mach_o(cmsys::ifstream& fin)
- {
- if(!read(fin, this->Header))
- {
+ {
+ if (!read(fin, this->Header)) {
return false;
- }
+ }
this->Header.cputype = swap(this->Header.cputype);
this->Header.cpusubtype = swap(this->Header.cpusubtype);
this->Header.filetype = swap(this->Header.filetype);
@@ -163,45 +153,40 @@ public:
this->Header.sizeofcmds = swap(this->Header.sizeofcmds);
this->Header.flags = swap(this->Header.flags);
- return read_load_commands(this->Header.ncmds,
- this->Header.sizeofcmds,
+ return read_load_commands(this->Header.ncmds, this->Header.sizeofcmds,
fin);
- }
+ }
+
protected:
T Header;
};
-
bool cmMachOHeaderAndLoadCommands::read_load_commands(uint32_t ncmds,
uint32_t sizeofcmds,
cmsys::ifstream& fin)
{
uint32_t size_read = 0;
this->LoadCommands.resize(ncmds);
- for(uint32_t i = 0; i<ncmds; i++)
- {
+ for (uint32_t i = 0; i < ncmds; i++) {
load_command lc;
- if(!peek(fin, lc))
- {
+ if (!peek(fin, lc)) {
return false;
- }
+ }
lc.cmd = swap(lc.cmd);
lc.cmdsize = swap(lc.cmdsize);
size_read += lc.cmdsize;
RawLoadCommand& c = this->LoadCommands[i];
c.LoadCommand.resize(lc.cmdsize);
- if(!read(fin, c.LoadCommand))
- {
+ if (!read(fin, c.LoadCommand)) {
return false;
- }
}
+ }
- if(size_read != sizeofcmds)
- {
+ if (size_read != sizeofcmds) {
this->LoadCommands.clear();
return false;
- }
+ }
return true;
}
@@ -233,126 +218,100 @@ cmMachOInternal::cmMachOInternal(const char* fname)
: Fin(fname)
{
// Quit now if the file could not be opened.
- if(!this->Fin || !this->Fin.get() )
- {
+ if (!this->Fin || !this->Fin.get()) {
this->ErrorMessage = "Error opening input file.";
return;
- }
+ }
- if(!this->Fin.seekg(0))
- {
+ if (!this->Fin.seekg(0)) {
this->ErrorMessage = "Error seeking to beginning of file.";
return;
- }
+ }
// Read the binary identification block.
uint32_t magic = 0;
- if(!peek(this->Fin, magic))
- {
+ if (!peek(this->Fin, magic)) {
this->ErrorMessage = "Error reading Mach-O identification.";
return;
- }
+ }
// Verify the binary identification.
- if(!(magic == MH_CIGAM ||
- magic == MH_MAGIC ||
- magic == MH_CIGAM_64 ||
- magic == MH_MAGIC_64 ||
- magic == FAT_CIGAM ||
- magic == FAT_MAGIC))
- {
+ if (!(magic == MH_CIGAM || magic == MH_MAGIC || magic == MH_CIGAM_64 ||
+ magic == MH_MAGIC_64 || magic == FAT_CIGAM || magic == FAT_MAGIC)) {
this->ErrorMessage = "File does not have a valid Mach-O identification.";
return;
- }
+ }
- if(magic == FAT_MAGIC || magic == FAT_CIGAM)
- {
+ if (magic == FAT_MAGIC || magic == FAT_CIGAM) {
// this is a universal binary
fat_header header;
- if(!read(this->Fin, header))
- {
+ if (!read(this->Fin, header)) {
this->ErrorMessage = "Error reading fat header.";
return;
- }
+ }
// read fat_archs
this->FatArchs.resize(OSSwapBigToHostInt32(header.nfat_arch));
- if(!read(this->Fin, this->FatArchs))
- {
+ if (!read(this->Fin, this->FatArchs)) {
this->ErrorMessage = "Error reading fat header archs.";
return;
- }
+ }
// parse each Mach-O file
- for(size_t i=0; i<this->FatArchs.size(); i++)
- {
+ for (size_t i = 0; i < this->FatArchs.size(); i++) {
const fat_arch& arch = this->FatArchs[i];
- if(!this->read_mach_o(OSSwapBigToHostInt32(arch.offset)))
- {
+ if (!this->read_mach_o(OSSwapBigToHostInt32(arch.offset))) {
return;
- }
}
}
- else
- {
+ } else {
// parse Mach-O file at the beginning of the file
this->read_mach_o(0);
- }
+ }
}
cmMachOInternal::~cmMachOInternal()
{
- for(size_t i=0; i<this->MachOList.size(); i++)
- {
+ for (size_t i = 0; i < this->MachOList.size(); i++) {
delete this->MachOList[i];
- }
+ }
}
bool cmMachOInternal::read_mach_o(uint32_t file_offset)
{
- if(!this->Fin.seekg(file_offset))
- {
+ if (!this->Fin.seekg(file_offset)) {
this->ErrorMessage = "Failed to locate Mach-O content.";
return false;
- }
+ }
uint32_t magic;
- if(!peek(this->Fin, magic))
- {
+ if (!peek(this->Fin, magic)) {
this->ErrorMessage = "Error reading Mach-O identification.";
return false;
- }
+ }
cmMachOHeaderAndLoadCommands* f = NULL;
- if(magic == MH_CIGAM || magic == MH_MAGIC)
- {
+ if (magic == MH_CIGAM || magic == MH_MAGIC) {
bool swap = false;
- if(magic == MH_CIGAM)
- {
+ if (magic == MH_CIGAM) {
swap = true;
- }
- f = new cmMachOHeaderAndLoadCommandsImpl<mach_header>(swap);
}
- else if(magic == MH_CIGAM_64 || magic == MH_MAGIC_64)
- {
+ f = new cmMachOHeaderAndLoadCommandsImpl<mach_header>(swap);
+ } else if (magic == MH_CIGAM_64 || magic == MH_MAGIC_64) {
bool swap = false;
- if(magic == MH_CIGAM_64)
- {
+ if (magic == MH_CIGAM_64) {
swap = true;
- }
- f = new cmMachOHeaderAndLoadCommandsImpl<mach_header_64>(swap);
}
+ f = new cmMachOHeaderAndLoadCommandsImpl<mach_header_64>(swap);
+ }
- if(f && f->read_mach_o(this->Fin))
- {
+ if (f && f->read_mach_o(this->Fin)) {
this->MachOList.push_back(f);
- }
- else
- {
+ } else {
delete f;
this->ErrorMessage = "Failed to read Mach-O header.";
return false;
- }
+ }
return true;
}
@@ -360,10 +319,10 @@ bool cmMachOInternal::read_mach_o(uint32_t file_offset)
//============================================================================
// External class implementation.
-cmMachO::cmMachO(const char* fname): Internal(0)
+cmMachO::cmMachO(const char* fname)
+ : Internal(0)
{
this->Internal = new cmMachOInternal(fname);
-
}
cmMachO::~cmMachO()
@@ -383,30 +342,25 @@ bool cmMachO::Valid() const
bool cmMachO::GetInstallName(std::string& install_name)
{
- if(this->Internal->MachOList.empty())
- {
+ if (this->Internal->MachOList.empty()) {
return false;
- }
+ }
// grab the first Mach-O and get the install name from that one
cmMachOHeaderAndLoadCommands* macho = this->Internal->MachOList[0];
- for(size_t i=0; i<macho->load_commands().size(); i++)
- {
- const cmMachOHeaderAndLoadCommands::RawLoadCommand &cmd =
+ for (size_t i = 0; i < macho->load_commands().size(); i++) {
+ const cmMachOHeaderAndLoadCommands::RawLoadCommand& cmd =
macho->load_commands()[i];
uint32_t lc_cmd = cmd.type(macho);
- if(lc_cmd == LC_ID_DYLIB ||
- lc_cmd == LC_LOAD_WEAK_DYLIB ||
- lc_cmd == LC_LOAD_DYLIB)
- {
- if(sizeof(dylib_command) < cmd.LoadCommand.size())
- {
+ if (lc_cmd == LC_ID_DYLIB || lc_cmd == LC_LOAD_WEAK_DYLIB ||
+ lc_cmd == LC_LOAD_DYLIB) {
+ if (sizeof(dylib_command) < cmd.LoadCommand.size()) {
uint32_t namelen = cmd.LoadCommand.size() - sizeof(dylib_command);
install_name.assign(&cmd.LoadCommand[sizeof(dylib_command)], namelen);
return true;
- }
}
}
+ }
return false;
}