Commit 36818759 authored by Artem Pyanykh's avatar Artem Pyanykh Committed by Marge Bot

Adjust section placement and relocation logic for Mach-O

1. Place each section on a separate page to ensure required
alignment (wastes lots ot space, needs to be improved).
2. Unwire relocation logic from macho sections (the most fiddly part
is adjusting internal relocations).

Other todos:
0. Add a test for section alignment.
1. Investigate 32bit relocations!
2. Fix memory leak in ZEROPAGE section allocation.
3. Fix creating redundant jump islands for GOT.
4. Investigate more compact section placement.
parent 3394a7cd
......@@ -1419,6 +1419,8 @@ preloadObjectFile (pathchar *path)
#endif /* RTS_LINKER_USE_MMAP */
IF_DEBUG(linker, debugBelch("loadObj: preloaded image at %p\n", (void *) image));
oc = mkOc(path, image, fileSize, true, NULL, misalignment);
#if defined(OBJFORMAT_MACHO)
......@@ -1440,7 +1442,7 @@ preloadObjectFile (pathchar *path)
static HsInt loadObj_ (pathchar *path)
{
ObjectCode* oc;
IF_DEBUG(linker, debugBelch("loadObj %" PATH_FMT "\n", path));
IF_DEBUG(linker, debugBelch("loadObj: %" PATH_FMT "\n", path));
/* debugBelch("loadObj %s\n", path ); */
......@@ -1611,6 +1613,8 @@ int ocTryLoad (ObjectCode* oc) {
// run init/init_array/ctors/mod_init_func
IF_DEBUG(linker, debugBelch("ocTryLoad: ocRunInit start\n"));
loading_obj = oc; // tells foreignExportStablePtr what to do
#if defined(OBJFORMAT_ELF)
r = ocRunInit_ELF ( oc );
......@@ -1814,8 +1818,8 @@ void freeProddableBlocks (ObjectCode *oc)
*/
void
addSection (Section *s, SectionKind kind, SectionAlloc alloc,
void* start, StgWord size, StgWord mapped_offset,
void* mapped_start, StgWord mapped_size)
void* start, StgWord size,
StgWord mapped_offset, void* mapped_start, StgWord mapped_size)
{
s->start = start; /* actual start of section in memory */
s->size = size; /* actual size of section in memory */
......
This diff is collapsed.
......@@ -6,13 +6,14 @@
#include "MachOTypes.h"
void ocInit_MachO ( ObjectCode* oc );
void ocDeinit_MachO ( ObjectCode* oc );
int ocVerifyImage_MachO ( ObjectCode* oc );
int ocGetNames_MachO ( ObjectCode* oc );
int ocResolve_MachO ( ObjectCode* oc );
int ocRunInit_MachO ( ObjectCode* oc );
int machoGetMisalignment( FILE * );
int ocAllocateExtras_MachO ( ObjectCode* oc );
void ocInit_MachO ( ObjectCode* oc );
void ocDeinit_MachO ( ObjectCode* oc );
int ocVerifyImage_MachO ( ObjectCode* oc );
size_t ocGetRequiredSpace_MachO ( ObjectCode* oc);
int ocGetNames_MachO ( ObjectCode* oc );
int ocResolve_MachO ( ObjectCode* oc );
int ocRunInit_MachO ( ObjectCode* oc );
int machoGetMisalignment ( FILE * );
int ocAllocateExtras_MachO ( ObjectCode* oc );
#include "EndPrivate.h"
......@@ -149,6 +149,7 @@ SymbolExtra* makeSymbolExtra( ObjectCode const* oc,
#endif /* powerpc_HOST_ARCH */
#if defined(x86_64_HOST_ARCH)
// jmp *-14(%rip)
// 0xFF 25 is opcode + ModRM of near absolute indirect jump
static uint8_t jmp[] = { 0xFF, 0x25, 0xF2, 0xFF, 0xFF, 0xFF };
extra->addr = target;
memcpy(extra->jumpIsland, jmp, 6);
......
TOP=../../..
include $(TOP)/mk/boilerplate.mk
include $(TOP)/mk/test.mk
unsigned_reloc_macho_64:
as -o unsigned_reloc_macho_64.o unsigned_reloc_macho_64.s
'$(TEST_HC)' $(TEST_HC_OPTS_NO_RTSOPTS) -v0 --make -no-rtsopts-suggestions -no-hs-main -o runner -debug runner.c
./runner unsigned_reloc_macho_64.o getAnswer
# -*-: mode: python -*-
test('unsigned_reloc_macho_64',
[
extra_files(['runner.c', 'unsigned_reloc_macho_64.s']),
unless(opsys('darwin'), skip),
unless(arch('x86_64'), skip)
],
run_command, ['$MAKE -s --no-print-directory unsigned_reloc_macho_64'])
#include <Rts.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define MAX_SYMNAME 256
typedef long (*fun_t)(void);
int main(int argc, char *argv[])
{
fun_t f;
int ok;
char * objpath;
hs_init(&argc, &argv);
initLinker();
if (argc < 3) {
errorBelch("usage: runner <objpath> <symname>");
exit(1);
}
objpath = argv[1];
#ifdef darwin_HOST_OS
char symname[MAX_SYMNAME + 1];
symname[0] = '_';
strncpy(&symname[1], argv[2], MAX_SYMNAME);
#else
char * symname = argv[2];
#endif
printf("Linking: path = %s, symname = %s\n", objpath, symname);
ok = loadObj(objpath);
if (!ok) {
errorBelch("loadObj(%s) failed", objpath);
exit(1);
}
ok = resolveObjs();
if (!ok) {
errorBelch("resolveObjs failed");
exit(1);
}
f = lookupSymbol(symname);
if (!f) {
errorBelch("lookupSymbol failed");
exit(1);
}
printf("%ld\n", f());
ok = unloadObj(objpath);
if (!ok) {
errorBelch("unloadObj(%s) failed", objpath);
exit(1);
}
return 0;
}
.text
.globl _getAnswer
_getAnswer:
mov L2(%rip), %rdx
movq (%rdx), %rax
ret
.data
L1: .quad 42
L2: .quad L1
Linking: path = unsigned_reloc_macho_64.o, symname = _getAnswer
42
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment