Commit 78c61acf authored by Artem Pyanykh's avatar Artem Pyanykh Committed by Marge Bot

Use segments for section layout

parent 36818759
......@@ -1195,6 +1195,7 @@ void freeObjectCode (ObjectCode *oc)
}
freeProddableBlocks(oc);
freeSegments(oc);
/* Free symbol_extras. On x86_64 Windows, symbol_extras are allocated
* alongside the image, so we don't need to free. */
......@@ -1279,6 +1280,8 @@ mkOc( pathchar *path, char *image, int imageSize,
oc->symbols = NULL;
oc->n_sections = 0;
oc->sections = NULL;
oc->n_segments = 0;
oc->segments = NULL;
oc->proddables = NULL;
oc->stable_ptrs = NULL;
#if defined(NEED_SYMBOL_EXTRAS)
......@@ -1840,3 +1843,50 @@ addSection (Section *s, SectionKind kind, SectionAlloc alloc,
start, (void*)((StgWord)start + size),
size, kind ));
}
/* -----------------------------------------------------------------------------
* Segment management
*/
void
initSegment (Segment *s, void *start, size_t size, SegmentProt prot, int n_sections)
{
s->start = start;
s->size = size;
s->prot = prot;
s->sections_idx = (int *)stgCallocBytes(n_sections, sizeof(int),
"initSegment(segment)");
s->n_sections = n_sections;
}
void freeSegments (ObjectCode *oc)
{
if (oc->segments != NULL) {
IF_DEBUG(linker, debugBelch("freeSegments: freeing %d segments\n", oc->n_segments));
for (int i = 0; i < oc->n_segments; i++) {
Segment *s = &oc->segments[i];
IF_DEBUG(linker, debugBelch("freeSegments: freeing segment %d at %p size %zu\n",
i, s->start, s->size));
stgFree(s->sections_idx);
s->sections_idx = NULL;
if (0 == s->size) {
IF_DEBUG(linker, debugBelch("freeSegment: skipping segment of 0 size\n"));
continue;
} else {
#if RTS_LINKER_USE_MMAP
CHECKM(0 == munmap(s->start, s->size), "freeSegments: failed to unmap memory");
#else
stgFree(s->start);
#endif
}
s->start = NULL;
}
stgFree(oc->segments);
oc->segments = NULL;
}
}
......@@ -62,6 +62,11 @@ typedef
}
SectionAlloc;
typedef enum {
SEGMENT_PROT_RX,
SEGMENT_PROT_RW
} SegmentProt;
/*
* Note [No typedefs for customizable types]
* Some pointer-to-struct types are defined opaquely
......@@ -103,6 +108,18 @@ typedef
}
ProddableBlock;
typedef struct _Segment {
void *start; /* page aligned start address of a segment */
size_t size; /* page rounded size of a segment */
SegmentProt prot; /* mem protection to set after all symbols were
* resolved */
int *sections_idx; /* an array of section indexes assigned to this segment */
int n_sections;
} Segment;
/* todo (AP): add freeSegments */
/*
* We must keep track of the StablePtrs that are created for foreign
* exports by constructor functions when the module is loaded, so that
......@@ -179,9 +196,14 @@ typedef struct _ObjectCode {
/* The section-kind entries for this object module. Linked
list. */
/* fixme (AP): doesn't look like a linked list. On MachO it's an array, and
* generally Section struct doesn't have pointers to next. */
int n_sections;
Section* sections;
int n_segments;
Segment *segments;
/* Allow a chain of these things */
struct _ObjectCode * next;
......@@ -312,6 +334,9 @@ ObjectCode* mkOc( pathchar *path, char *image, int imageSize,
int misalignment
);
void initSegment(Segment *s, void *start, size_t size, SegmentProt prot, int n_sections);
void freeSegments(ObjectCode *oc);
/* MAP_ANONYMOUS is MAP_ANON on some systems,
e.g. OS X (before Sierra), OpenBSD etc */
#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
......
......@@ -97,13 +97,13 @@ stgReallocBytes (void *p, size_t n, char *msg)
}
void *
stgCallocBytes (size_t n, size_t m, char *msg)
stgCallocBytes (size_t count, size_t size, char *msg)
{
void *space;
if ((space = calloc(n, m)) == NULL) {
if ((space = calloc(count, size)) == NULL) {
/* don't fflush(stdout); WORKAROUND bug in Linux glibc */
rtsConfig.mallocFailHook((W_) n*m, msg);
rtsConfig.mallocFailHook((W_) count*size, msg);
stg_exit(EXIT_INTERNAL_ERROR);
}
return space;
......
......@@ -22,7 +22,7 @@ void *stgMallocBytes(size_t n, char *msg)
void *stgReallocBytes(void *p, size_t n, char *msg);
void *stgCallocBytes(size_t n, size_t m, char *msg)
void *stgCallocBytes(size_t count, size_t size, char *msg)
GNUC3_ATTRIBUTE(__malloc__);
char *stgStrndup(const char *s, size_t n);
......
This diff is collapsed.
......@@ -9,11 +9,13 @@
void ocInit_MachO ( ObjectCode* oc );
void ocDeinit_MachO ( ObjectCode* oc );
int ocVerifyImage_MachO ( ObjectCode* oc );
size_t ocGetRequiredSpace_MachO ( ObjectCode* oc);
int ocBuildSegments_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 );
SectionKind getSectionKind_MachO ( MachOSection *macho );
#include "EndPrivate.h"
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