@@ -0,0 +1,189 @@
1+diff --git a/rts/Linker.c b/rts/Linker.c
2+index 10b0764..7d473f8 100644
3+--- a/rts/Linker.c
4++++ b/rts/Linker.c
5+@@ -273,7 +273,7 @@ int ghciInsertSymbolTable(
6+ RtsSymbolInfo *pinfo = lookupStrHashTable(table, key);
7+ if (!pinfo) /* new entry */
8+ {
9+- pinfo = stgMallocBytes(sizeof (*pinfo), "ghciInsertToSymbolTable");
10++ pinfo = stgCallocBytes(1, sizeof (*pinfo), "ghciInsertToSymbolTable");
11+ pinfo->value = data;
12+ pinfo->owner = owner;
13+ pinfo->weak = weak;
14+@@ -1329,7 +1329,7 @@ mkOc( pathchar *path, char *image, int imageSize,
15+ ObjectCode* oc;
16+
17+ IF_DEBUG(linker, debugBelch("mkOc: start\n"));
18+- oc = stgMallocBytes(sizeof(ObjectCode), "mkOc(oc)");
19++ oc = stgCallocBytes(1, sizeof(ObjectCode), "mkOc(oc)");
20+
21+ oc->info = NULL;
22+
23+@@ -1496,12 +1496,12 @@ preloadObjectFile (pathchar *path)
24+ // reading the file, and then we misalign image on purpose so
25+ // that the actual sections end up aligned again.
26+ misalignment = machoGetMisalignment(f);
27+- image = stgMallocBytes(fileSize + misalignment, "loadObj(image)");
28++ image = stgCallocBytes(1, fileSize + misalignment, "loadObj(image)");
29+ image += misalignment;
30+
31+ # else /* !defined(darwin_HOST_OS) */
32+
33+- image = stgMallocBytes(fileSize, "loadObj(image)");
34++ image = stgCallocBytes(1, fileSize, "loadObj(image)");
35+
36+ #endif
37+
38+diff --git a/rts/LinkerInternals.h b/rts/LinkerInternals.h
39+index f326a84..a846bf5 100644
40+--- a/rts/LinkerInternals.h
41++++ b/rts/LinkerInternals.h
42+@@ -209,6 +209,10 @@ typedef struct _ObjectCode {
43+ int n_segments;
44+ Segment *segments;
45+
46++ // COMMON section
47++ void * common_mem;
48++ unsigned long common_size;
49++
50+ //
51+ // Garbage collection fields
52+ //
53+diff --git a/rts/linker/Elf.c b/rts/linker/Elf.c
54+index fdfe87a..c3f9110 100644
55+--- a/rts/linker/Elf.c
56++++ b/rts/linker/Elf.c
57+@@ -325,6 +325,15 @@ ocDeinit_ELF(ObjectCode * oc)
58+ stgFree(oc->info);
59+ oc->info = NULL;
60+ }
61++ if(NULL != oc->common_mem) {
62++#if RTS_LINKER_USE_MMAP
63++ munmap(oc->common_mem, oc->common_size);
64++#else
65++ stgFree(oc->common_mem);
66++#endif
67++ }
68++ oc->common_mem = NULL;
69++ oc->common_size = 0;
70+ }
71+
72+ /*
73+@@ -861,14 +870,17 @@ ocGetNames_ELF ( ObjectCode* oc )
74+ for (size_t j = 0; j < symTab->n_symbols; j++) {
75+ ElfSymbol *symbol = &symTab->symbols[j];
76+ if (SHN_COMMON == symTab->symbols[j].elf_sym->st_shndx) {
77+- common_size += symbol->elf_sym->st_size;
78++ // st_value holds the alignment. Adding alignment always
79++ // should give us some wiggle room to get alignment right.
80++ common_size += symbol->elf_sym->st_size + symbol->elf_sym->st_value;
81+ }
82+ }
83+ }
84+- void * common_mem = NULL;
85++ oc->common_mem = NULL;
86++ oc->common_size = common_size;
87+ if(common_size > 0) {
88+- common_mem = mmapAnonForLinker(common_size);
89+- if (common_mem == NULL) {
90++ oc->common_mem = mmapAnonForLinker(common_size);
91++ if (oc->common_mem == NULL) {
92+ barf("ocGetNames_ELF: Failed to allocate memory for SHN_COMMONs");
93+ }
94+ }
95+@@ -909,9 +921,10 @@ ocGetNames_ELF ( ObjectCode* oc )
96+ if (shndx == SHN_COMMON) {
97+ isLocal = false;
98+ ASSERT(common_used < common_size);
99+- ASSERT(common_mem);
100+- symbol->addr = (void*)((uintptr_t)common_mem + common_used);
101+- common_used += symbol->elf_sym->st_size;
102++ ASSERT(oc->common_mem);
103++ int alignment = symbol->elf_sym->st_value-1;
104++ symbol->addr = (void*)(((uintptr_t)oc->common_mem + common_used + alignment) & ~alignment);
105++ common_used = (uintptr_t)symbol->addr - (uintptr_t)oc->common_mem + symbol->elf_sym->st_size;
106+ ASSERT(common_used <= common_size);
107+
108+ IF_DEBUG(linker,
109+@@ -925,7 +938,9 @@ ocGetNames_ELF ( ObjectCode* oc )
110+ || ELF_ST_BIND(symbol->elf_sym->st_info) == STB_WEAK
111+ )
112+ /* and not an undefined symbol */
113+- && shndx != SHN_UNDEF
114++ && (shndx != SHN_UNDEF
115++ /* unless it's weak */
116++ || (shndx == SHN_UNDEF && ELF_ST_BIND(symbol->elf_sym->st_info) == STB_WEAK))
117+ /* and not in a "special section" */
118+ && (shndx < SHN_LORESERVE
119+ #if defined(SHN_XINDEX)
120+@@ -963,33 +978,20 @@ ocGetNames_ELF ( ObjectCode* oc )
121+ isWeak = ELF_ST_BIND(symbol->elf_sym->st_info)
122+ == STB_WEAK;
123+ }
124+- }
125+-
126+- /* And the decision is ... */
127+-
128+- if (symbol->addr != NULL) {
129+- ASSERT(nm != NULL);
130+- /* Acquire! */
131+- if (!isLocal) {
132+-
133+- if (isWeak == HS_BOOL_TRUE) {
134+- setWeakSymbol(oc, nm);
135+- }
136+- if (!ghciInsertSymbolTable(oc->fileName, symhash,
137+- nm, symbol->addr, isWeak, oc)
138+- ) {
139+- goto fail;
140+- }
141+- oc->symbols[curSymbol++].name = nm;
142+- oc->symbols[curSymbol].addr = symbol->addr;
143+- }
144+- } else {
145+- /* Skip. */
146++ } else if (ELF_ST_BIND(symbol->elf_sym->st_info) == STB_WEAK
147++ && shndx == SHN_UNDEF
148++ && (ELF_ST_TYPE(symbol->elf_sym->st_info) == STT_FUNC
149++ || ELF_ST_TYPE(symbol->elf_sym->st_info) == STT_OBJECT
150++ || ELF_ST_TYPE(symbol->elf_sym->st_info) == STT_NOTYPE)) {
151++ symbol->addr = NULL;
152++ isLocal = false;
153++ isWeak = true;
154++ } else {
155++ /* skip this symbol */
156+ IF_DEBUG(linker,
157+ debugBelch("skipping `%s'\n",
158+ nm)
159+ );
160+-
161+ /*
162+ debugBelch(
163+ "skipping bind = %d, type = %d, secno = %d `%s'\n",
164+@@ -999,7 +1001,24 @@ ocGetNames_ELF ( ObjectCode* oc )
165+ nm
166+ );
167+ */
168+- }
169++ continue;
170++ }
171++
172++ /* And the decision is ... */
173++ ASSERT(nm != NULL);
174++ /* Acquire! */
175++ if (!isLocal) {
176++
177++ if (isWeak == HS_BOOL_TRUE) {
178++ setWeakSymbol(oc, nm);
179++ }
180++ if (!ghciInsertSymbolTable(oc->fileName, symhash,
181++ nm, symbol->addr, isWeak, oc)) {
182++ goto fail;
183++ }
184++ oc->symbols[curSymbol++].name = nm;
185++ oc->symbols[curSymbol].addr = symbol->addr;
186++ }
187+ }
188+ }
189+ }