Permalink
Browse files

Add a constructor to MachO as a preparation for fat binary support.

  • Loading branch information...
1 parent 77b6cc8 commit 4715f51617e5f0f81f3df5a4f168b60b71f3dc85 @shinh committed Mar 10, 2011
Showing with 19 additions and 4 deletions.
  1. +16 −4 mach-o.cc
  2. +3 −0 mach-o.h
View
20 mach-o.cc
@@ -25,6 +25,7 @@
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
+#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
@@ -188,16 +189,27 @@ void MachO::readBind(const uint8_t* p, const uint8_t* end) {
}
MachO::MachO(const char* filename) {
- fd_ = open(filename, O_RDONLY);
- if (fd_ < 0) {
+ int fd = open(filename, O_RDONLY);
+ if (fd < 0) {
fprintf(stderr, "open %s: %s\n", filename, strerror(errno));
exit(1);
}
+ init(fd, 0, 0);
+}
+
+MachO::MachO(int fd, size_t offset, size_t len) {
+ init(fd, offset, len);
+}
- off_t len = lseek(fd_, 0, SEEK_END);
+void MachO::init(int fd, size_t offset, size_t len) {
+ assert(fd);
+ fd_ = fd;
+ if (!len) {
+ len = lseek(fd_, 0, SEEK_END);
+ }
char* bin = reinterpret_cast<char*>(
mmap(NULL, len,
- PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd_, 0));
+ PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd_, offset));
base_ = bin;
mach_header* header = reinterpret_cast<mach_header*>(bin);
View
3 mach-o.h
@@ -47,6 +47,7 @@ class MachO {
};
explicit MachO(const char* filename);
+ MachO(int fd, size_t offset, size_t len);
~MachO();
@@ -69,6 +70,8 @@ class MachO {
int fd() const { return fd_; }
private:
+ // If len is 0, the size of file will be used as len.
+ void init(int fd, size_t offset, size_t len);
void readBind(const uint8_t* p, const uint8_t* end);
vector<segment_command_64*> segments64_;

0 comments on commit 4715f51

Please sign in to comment.