Merge pull request #1 from OznOg/master

Some cleanup, strmode, and vm_msync
This commit is contained in:
Shinichiro Hamaji 2011-07-02 19:52:35 -07:00
commit 011b2973c6
5 changed files with 225 additions and 26 deletions

View File

@ -77,8 +77,8 @@ ld-mac: ld-mac.o mach-o.o fat.o log.o
$(CXX) $^ -o $@ -g -ldl -lpthread $(GCC_EXTRA_FLAGS)
# TODO(hamaji): autotoolize?
libmac.so: libmac/mac.o
$(CC) -shared $^ -o $@ -lcrypto $(GCC_EXTRA_FLAGS)
libmac.so: libmac/mac.o libmac/strmode.c
$(CC) -shared $^ $(CFLAGS) -o $@ -lcrypto $(GCC_EXTRA_FLAGS)
dist:
cd /tmp && rm -fr maloader-$(VERSION) && git clone git@github.com:shinh/maloader.git && rm -fr maloader/.git && mv maloader maloader-$(VERSION) && tar -cvzf maloader-$(VERSION).tar.gz maloader-$(VERSION)

View File

@ -172,7 +172,8 @@ static char* g_darwin_executable_path;
static Timer g_timer;
// TODO(hamaji): Need a static type...
static void* g_loader;
class MachOLoader;
static MachOLoader* g_loader;
static void initRename() {
#define RENAME(src, dst) g_rename.insert(make_pair(#src, #dst));
@ -931,7 +932,7 @@ static void* ld_mac_dlopen(const char* filename, int flag) {
auto_ptr<MachO> dylib_mach(MachO::read(filename, ARCH_NAME));
// TODO(hamaji): Consider 32bit.
MachOLoader* loader = (MachOLoader*)g_loader;
MachOLoader* loader = g_loader;
CHECK(loader);
Exports* exports = new Exports();
loader->load(*dylib_mach, exports);

View File

@ -265,17 +265,27 @@ int mach_port_deallocate() {
return 0;
}
/* FIXME implement vm_function corectly.
* OznOg Obviosly, all this remain completelly wrong because completely void.
* This functions allow programs to start correctly and usually to run (almost)
* correctly, but the memory managment remains wrong. I do not really have good ideas
* to handle all this without reimplementing the whole memory managment now. Feel free
* to give me some good ideas.
* I do not think implementing vm_allocate vm_deallocate and vm_msync is a priority
* but I guess some programs really need them to work correctly.
*/
int vm_msync(int target_task, void** addr, size_t size, int flags) {
return 0;
}
int vm_allocate(int target_task, void** addr, size_t size, int flags) {
//fprintf(stderr, "vm_allocate: size=%lu\n", size);
*addr = calloc(size, 1);
return 0;
}
int vm_deallocate() {
// TODO(hamaji): munmap, maybe
//fprintf(stderr, "vm_deallocate()\n");
return 0;
//abort();
}
void *__darwin_mmap(void *addr, size_t length, int prot, int flags,

168
libmac/strmode.c Normal file
View File

@ -0,0 +1,168 @@
/* $NetBSD: strmode.c,v 1.5 2004/08/23 03:32:12 jlam Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <unistd.h>
void
strmode(mode, p)
mode_t mode;
char *p;
{
assert(p != NULL);
/* print type */
switch (mode & S_IFMT) {
case S_IFDIR: /* directory */
*p++ = 'd';
break;
case S_IFCHR: /* character special */
*p++ = 'c';
break;
case S_IFBLK: /* block special */
*p++ = 'b';
break;
case S_IFREG: /* regular */
#ifdef S_ARCH2
if ((mode & S_ARCH2) != 0) {
*p++ = 'A';
} else if ((mode & S_ARCH1) != 0) {
*p++ = 'a';
} else {
#endif
*p++ = '-';
#ifdef S_ARCH2
}
#endif
break;
case S_IFLNK: /* symbolic link */
*p++ = 'l';
break;
#ifdef S_IFSOCK
case S_IFSOCK: /* socket */
*p++ = 's';
break;
#endif
#ifdef S_IFIFO
case S_IFIFO: /* fifo */
*p++ = 'p';
break;
#endif
#ifdef S_IFWHT
case S_IFWHT: /* whiteout */
*p++ = 'w';
break;
#endif
#ifdef S_IFDOOR
case S_IFDOOR: /* door */
*p++ = 'D';
break;
#endif
default: /* unknown */
*p++ = '?';
break;
}
/* usr */
if (mode & S_IRUSR)
*p++ = 'r';
else
*p++ = '-';
if (mode & S_IWUSR)
*p++ = 'w';
else
*p++ = '-';
switch (mode & (S_IXUSR | S_ISUID)) {
case 0:
*p++ = '-';
break;
case S_IXUSR:
*p++ = 'x';
break;
case S_ISUID:
*p++ = 'S';
break;
case S_IXUSR | S_ISUID:
*p++ = 's';
break;
}
/* group */
if (mode & S_IRGRP)
*p++ = 'r';
else
*p++ = '-';
if (mode & S_IWGRP)
*p++ = 'w';
else
*p++ = '-';
switch (mode & (S_IXGRP | S_ISGID)) {
case 0:
*p++ = '-';
break;
case S_IXGRP:
*p++ = 'x';
break;
case S_ISGID:
*p++ = 'S';
break;
case S_IXGRP | S_ISGID:
*p++ = 's';
break;
}
/* other */
if (mode & S_IROTH)
*p++ = 'r';
else
*p++ = '-';
if (mode & S_IWOTH)
*p++ = 'w';
else
*p++ = '-';
switch (mode & (S_IXOTH | S_ISVTX)) {
case 0:
*p++ = '-';
break;
case S_IXOTH:
*p++ = 'x';
break;
case S_ISVTX:
*p++ = 'T';
break;
case S_IXOTH | S_ISVTX:
*p++ = 't';
break;
}
*p++ = ' '; /* will be a '+' if ACL's implemented */
*p = '\0';
}

View File

@ -194,6 +194,10 @@ void MachOImpl::readSegment(char* cmds_ptr,
int section_type = sec.flags & SECTION_TYPE;
switch (section_type) {
case S_REGULAR:
/* Regular section: nothing to do */
break;
case S_MOD_INIT_FUNC_POINTERS: {
for (uint64_t p = sec.addr; p < sec.addr + sec.size; p += ptrsize_) {
init_funcs_.push_back(p);
@ -205,9 +209,28 @@ void MachOImpl::readSegment(char* cmds_ptr,
bind_sections->push_back(sections + j);
break;
}
default:
case S_ZEROFILL:
case S_CSTRING_LITERALS:
case S_4BYTE_LITERALS:
case S_8BYTE_LITERALS:
case S_LITERAL_POINTERS:
case S_SYMBOL_STUBS:
case S_MOD_TERM_FUNC_POINTERS:
// TODO(hamaji): Support term_funcs.
;
case S_COALESCED:
case S_GB_ZEROFILL:
case S_INTERPOSING:
case S_16BYTE_LITERALS:
case S_DTRACE_DOF:
case S_LAZY_DYLIB_SYMBOL_POINTERS:
LOGF("FIXME: section type %d will not be handled for %s in %s\n",
section_type, sec.sectname, sec.segname);
break;
default:
fprintf(stderr, "Unknown section type: %d\n", section_type);
abort();
break;
}
}
}
@ -524,10 +547,9 @@ MachOImpl::MachOImpl(const char* filename, int fd, size_t offset, size_t len,
exit(1);
}
char* cmds_ptr = bin + sizeof(mach_header);
if (is64_) {
cmds_ptr += sizeof(uint32_t);
}
struct load_command* cmds_ptr = reinterpret_cast<struct load_command*>(
bin + (is64_ ? sizeof(mach_header_64)
: sizeof(mach_header)));
uint32_t* symtab = NULL;
uint32_t* dysyms = NULL;
@ -536,20 +558,19 @@ MachOImpl::MachOImpl(const char* filename, int fd, size_t offset, size_t len,
vector<section_64*> bind_sections_64;
vector<section*> bind_sections_32;
for (uint32_t i = 0; i < header->ncmds; i++) {
uint32_t cmd = *reinterpret_cast<uint32_t*>(cmds_ptr);
LOGF("%x\n", cmd);
for (uint32_t ii = 0; ii < header->ncmds; ii++) {
LOGF("cmd type:%x\n", cmds_ptr->cmd);
switch (cmd) {
switch (cmds_ptr->cmd) {
case LC_SEGMENT_64: {
readSegment<segment_command_64, section_64>(
cmds_ptr, &segments64_, &bind_sections_64);
(char *)cmds_ptr, &segments64_, &bind_sections_64);
break;
}
case LC_SEGMENT: {
readSegment<segment_command, section>(
cmds_ptr, &segments_, &bind_sections_32);
(char *)cmds_ptr, &segments_, &bind_sections_32);
break;
}
@ -702,9 +723,8 @@ MachOImpl::MachOImpl(const char* filename, int fd, size_t offset, size_t len,
}
case LC_LOAD_DYLINKER: {
lc_str name = *reinterpret_cast<lc_str*>(
cmds_ptr + sizeof(uint32_t) * 2);
LOGF("dylinker: %s\n", cmds_ptr + name.offset);
lc_str name = reinterpret_cast<struct dylinker_command*>(cmds_ptr)->name;
LOGF("dylinker: %s\n", (char *)cmds_ptr + name.offset);
break;
}
@ -728,15 +748,15 @@ MachOImpl::MachOImpl(const char* filename, int fd, size_t offset, size_t len,
}
case LC_LOAD_DYLIB: {
dylib* lib = reinterpret_cast<dylib*>(cmds_ptr + sizeof(uint32_t) * 2);
LOGF("dylib: %s\n", cmds_ptr + lib->name.offset);
dylibs_.push_back(cmds_ptr + lib->name.offset);
dylib* lib = &reinterpret_cast<dylib_command*>(cmds_ptr)->dylib;
LOGF("dylib: '%s'\n", (char *)cmds_ptr + lib->name.offset);
dylibs_.push_back((char *)cmds_ptr + lib->name.offset);
break;
}
}
cmds_ptr += reinterpret_cast<uint32_t*>(cmds_ptr)[1];
cmds_ptr = reinterpret_cast<load_command*>(reinterpret_cast<char *>(cmds_ptr) + cmds_ptr->cmdsize);
}
LOGF("%p vs %p\n", cmds_ptr, bin + mapped_size_);