]> Cypherpunks repositories - gostls13.git/commitdiff
6l, 8l: fix Mach-O binaries with many dynamic libraries
authorRuss Cox <rsc@golang.org>
Tue, 24 May 2011 23:50:13 +0000 (19:50 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 24 May 2011 23:50:13 +0000 (19:50 -0400)
R=ken2
CC=golang-dev
https://golang.org/cl/4529084

src/cmd/6l/obj.c
src/cmd/8l/obj.c
src/cmd/ld/macho.c
src/cmd/ld/macho.h

index d53814a74539805c7346a6dffd791f755adb3020..e3191bb4dc075ebc85abfccf14f11b14ec1de1c0 100644 (file)
@@ -177,7 +177,7 @@ main(int argc, char *argv[])
                 */
                tlsoffset = 0x8a0;
                machoinit();
-               HEADR = MACHORESERVE;
+               HEADR = INITIAL_MACHO_HEADR;
                if(INITRND == -1)
                        INITRND = 4096;
                if(INITTEXT == -1)
index f84a30f39056f684ccc3b4cb7742ffb64263b2c7..440dcb77f8154c791a66d9f07bba4771cdcb5547 100644 (file)
@@ -218,7 +218,7 @@ main(int argc, char *argv[])
                 */
                tlsoffset = 0x468;
                machoinit();
-               HEADR = MACHORESERVE;
+               HEADR = INITIAL_MACHO_HEADR;
                if(INITTEXT == -1)
                        INITTEXT = 4096+HEADR;
                if(INITDAT == -1)
index 01349bb10a59668fe03f49072764b1df9b584c5b..0b12ac17bf1e4b379ebc5fe7359561ef7733764d 100644 (file)
@@ -17,6 +17,14 @@ static       MachoSeg        seg[16];
 static MachoDebug      xdebug[16];
 static int     nload, mload, nseg, ndebug, nsect;
 
+// Amount of space left for adding load commands
+// that refer to dynamic libraries.  Because these have
+// to go in the Mach-O header, we can't just pick a
+// "big enough" header size.  The initial header is 
+// one page, the non-dynamic library stuff takes
+// up about 1300 bytes; we overestimate that as 2k.
+static int     load_budget = INITIAL_MACHO_HEADR - 2*1024;
+
 void
 machoinit(void)
 {
@@ -267,6 +275,17 @@ domacho(void)
 void
 machoadddynlib(char *lib)
 {
+       // Will need to store the library name rounded up
+       // and 24 bytes of header metadata.  If not enough
+       // space, grab another page of initial space at the
+       // beginning of the output file.
+       load_budget -= (strlen(lib)+7)/8*8 + 24;
+       if(load_budget < 0) {
+               HEADR += 4096;
+               INITTEXT += 4096;
+               load_budget += 4096;
+       }
+
        if(ndylib%32 == 0) {
                dylib = realloc(dylib, (ndylib+32)*sizeof dylib[0]);
                if(dylib == nil) {
@@ -463,8 +482,8 @@ asmbmacho(void)
        }
 
        a = machowrite();
-       if(a > MACHORESERVE)
-               diag("MACHORESERVE too small: %d > %d", a, MACHORESERVE);
+       if(a > HEADR)
+               diag("HEADR too small: %d > %d", a, HEADR);
 }
 
 vlong
index 4cc7edc80db0cf7077dd23272d3d198df7ed7668..f5510415023fefa3095be79c1b39745b5f907b15 100644 (file)
@@ -63,7 +63,7 @@ void  machoinit(void);
  * for Header, PHeaders, and SHeaders.
  * May waste some.
  */
-#define        MACHORESERVE    3*1024
+#define        INITIAL_MACHO_HEADR     4*1024
 
 enum {
        MACHO_CPU_AMD64 = (1<<24)|7,