Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to FreeBSD #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ VERSION=0.4
BITS=64

GCC_EXTRA_FLAGS=-m$(BITS)
GCCFLAGS+=-g -Iinclude -Wall -MMD -fno-omit-frame-pointer -O $(GCC_EXTRA_FLAGS)
GCCFLAGS+=-g -Iinclude -Wall -MMD -fno-omit-frame-pointer -O1 $(GCC_EXTRA_FLAGS)
ifeq ($(USE_LIBCXX), 1)
GCCFLAGS+=-stdlib=libc++ -DUSE_LIBCXX
CXX_LDFLAGS+=-lc++ -lsupc++
CC=clang
CXX=clang++
endif
CXXFLAGS=$(GCCFLAGS) -W -Werror --std=c++11
CXXFLAGS=$(GCCFLAGS) -Wextra -Werror --std=c++11
CFLAGS=$(GCCFLAGS) -fPIC

EXES=libmac.so extract macho2elf ld-mac
Expand All @@ -30,6 +30,7 @@ MAC_CC=PATH=$(MAC_BIN_DIR) ./ld-mac $(MAC_BIN_DIR)/gcc --sysroot=$(MAC_TOOL_DIR)
MAC_CXX=PATH=$(MAC_BIN_DIR) ./ld-mac $(MAC_BIN_DIR)/g++ --sysroot=$(MAC_TOOL_DIR)
MAC_OTOOL=./ld-mac $(MAC_BIN_DIR)/otool
MAC_TARGETS=ld-mac $(MACBINS) $(MACTXTS)
DL_LIBS=-ldl
else
MAC_CC=$(CC)
MAC_CXX=$(CXX)
Expand Down Expand Up @@ -79,13 +80,13 @@ $(MACTXTS): %.txt: %.bin
# touch $@

extract: extract.o fat.o
$(CXX) $^ -o $@ -g -I. -W -Wall $(GCC_EXTRA_FLAGS) $(CXX_LDFLAGS)
$(CXX) $^ -o $@ -g -I. -Wall $(GCC_EXTRA_FLAGS) $(CXX_LDFLAGS)

macho2elf: macho2elf.o mach-o.o fat.o log.o
$(CXX) $^ -o $@ -g $(GCC_EXTRA_FLAGS) $(CXX_LDFLAGS)

ld-mac: ld-mac.o mach-o.o fat.o log.o
$(CXX) -v $^ -o $@ -g -ldl -lpthread $(GCC_EXTRA_FLAGS) $(CXX_LDFLAGS)
$(CXX) -v $^ -g $(GCC_EXTRA_FLAGS) $(CXX_LDFLAGS) -o $@ $(DL_LIBS) -lpthread $(LIBS)

# TODO(hamaji): autotoolize?
libmac.so: libmac/mac.o libmac/strmode.c
Expand Down
8 changes: 8 additions & 0 deletions include/_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,22 @@
//#include <sys/_types.h>

#if __GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7
#ifndef __strfmonlike
#define __strfmonlike(fmtarg, firstvararg) \
__attribute__((__format__ (__strfmon__, fmtarg, firstvararg)))
#endif
#ifndef __strftimelike
#define __strftimelike(fmtarg) \
__attribute__((__format__ (__strftime__, fmtarg, 0)))
#endif
#else
#ifndef __strfmonlike
#define __strfmonlike(fmtarg, firstvararg)
#endif
#ifndef __strftimelike
#define __strftimelike(fmtarg)
#endif
#endif

typedef int __darwin_nl_item;
typedef int __darwin_wctrans_t;
Expand Down
26 changes: 13 additions & 13 deletions include/runetype.h → include/mac-runetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@
* @(#)runetype.h 8.1 (Berkeley) 6/2/93
*/

#ifndef _RUNETYPE_H_
#define _RUNETYPE_H_
#ifndef _MAC_RUNETYPE_H_
#define _MAC_RUNETYPE_H_

#include <_types.h>
#include <wchar.h>

typedef wchar_t __darwin_rune_t;
typedef size_t __darwin_size_t;

#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#if 0 && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))

#ifndef _SIZE_T
#define _SIZE_T
Expand Down Expand Up @@ -87,12 +87,12 @@ typedef struct {
__darwin_rune_t __max; /* Last rune (inclusive) of the range */
__darwin_rune_t __map; /* What first maps to in maps */
uint32_t *__types; /* Array of types in range */
} _RuneEntry;
} _MacRuneEntry;

typedef struct {
int __nranges; /* Number of ranges stored */
_RuneEntry *__ranges; /* Pointer to the ranges */
} _RuneRange;
_MacRuneEntry *__ranges; /* Pointer to the ranges */
} _MacRuneRange;

typedef struct {
char __name[14]; /* CHARCLASS_NAME_MAX = 14 */
Expand All @@ -116,9 +116,9 @@ typedef struct {
* Their data is actually contiguous with this structure so as to make
* it easier to read/write from/to disk.
*/
_RuneRange __runetype_ext;
_RuneRange __maplower_ext;
_RuneRange __mapupper_ext;
_MacRuneRange __runetype_ext;
_MacRuneRange __maplower_ext;
_MacRuneRange __mapupper_ext;

void *__variable; /* Data which depends on the encoding */
int __variable_len; /* how long that data is */
Expand All @@ -128,13 +128,13 @@ typedef struct {
*/
int __ncharclasses;
_RuneCharClass *__charclasses;
} _RuneLocale;
} _MacRuneLocale;

#define _RUNE_MAGIC_A "RuneMagA" /* Indicates version A of RuneLocale */

__BEGIN_DECLS
extern _RuneLocale _DefaultRuneLocale;
extern _RuneLocale *_CurrentRuneLocale;
extern _MacRuneLocale _MacDefaultRuneLocale;
extern _MacRuneLocale *_MacCurrentRuneLocale;
__END_DECLS

#endif /* !_RUNETYPE_H_ */
#endif /* !_MAC_RUNETYPE_H_ */
24 changes: 22 additions & 2 deletions ld-mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

// A Mach-O loader for linux.

#include <sys/param.h>
#include <assert.h>
#include <dlfcn.h>
#include <err.h>
Expand All @@ -43,6 +44,9 @@
#include <time.h>
#include <ucontext.h>
#include <unistd.h>
#ifdef BSD
#include <sys/sysctl.h>
#endif

#include <algorithm>
#include <iostream>
Expand Down Expand Up @@ -444,7 +448,7 @@ class MachOLoader {
void* mapped = mmap((void*)(vmaddr + filesize),
vmsize - filesize, prot,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
0, 0);
-1, 0);
if (mapped == MAP_FAILED) {
err(1, "%s mmap(anon) failed", mach.filename().c_str());
}
Expand Down Expand Up @@ -841,7 +845,7 @@ void MachOLoader::boot(
" push %%eax;\n"
" jmp *%0;\n"
// TODO(hamaji): Fix parameters
::"r"(entry), "r"(argc), "r"(argv + argc), "g"(envp)
::"r"((uint32_t)entry), "r"(argc), "r"(argv + argc), "g"(envp)
:"%eax", "%edx");
#endif
}
Expand Down Expand Up @@ -879,13 +883,21 @@ extern "C" {
/* signal handler for fatal errors */
static void handleSignal(int signum, siginfo_t* siginfo, void* vuc) {
ucontext_t *uc = (ucontext_t*)vuc;
#ifdef __linux__
void* pc = (void*)uc->uc_mcontext.gregs[
#ifdef __x86_64__
REG_RIP
#else
REG_EIP
#endif
];
#else
#ifdef __x86_64__
void *pc = (void *)uc->uc_mcontext.mc_rip;
#else
void *pc = (void *)uc->uc_mcontext.mc_eip;
#endif
#endif

fprintf(stderr, "%s(%d) %d (@%p) PC: %p\n\n",
strsignal(signum), signum, siginfo->si_code, siginfo->si_addr, pc);
Expand Down Expand Up @@ -949,11 +961,19 @@ static bool loadLibMac(const char* mypath) {

static void initLibMac() {
char mypath[PATH_MAX + 1];
#ifdef __linux__
ssize_t l = readlink("/proc/self/exe", mypath, PATH_MAX);
if (l < 0) {
err(1, "readlink for /proc/self/exe");
}
mypath[l] = '\0';
#else
size_t len = sizeof mypath;
int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
if(sysctl(mib, 4, mypath, &len, NULL, 0) < 0) {
err(1, "sysctl for kern.proc.pathname");
}
#endif

if (!loadLibMac(mypath)) {
fprintf(stderr, "libmac not found\n");
Expand Down
Loading