Andreas Klebinger pushed to branch wip/andreask/linker_fix at Glasgow Haskell Compiler / GHC

Commits:

7 changed files:

Changes:

  • rts/Linker.c
    ... ... @@ -1240,7 +1240,7 @@ void freeObjectCode (ObjectCode *oc)
    1240 1240
     
    
    1241 1241
     ObjectCode*
    
    1242 1242
     mkOc( ObjectType type, pathchar *path, char *image, int imageSize,
    
    1243
    -      bool mapped, pathchar *archiveMemberName, int misalignment ) {
    
    1243
    +      bool mapped, pathchar *archiveMemberName ) {
    
    1244 1244
        ObjectCode* oc;
    
    1245 1245
     
    
    1246 1246
     
    
    ... ... @@ -1294,7 +1294,6 @@ mkOc( ObjectType type, pathchar *path, char *image, int imageSize,
    1294 1294
        oc->bssEnd            = NULL;
    
    1295 1295
        oc->imageMapped       = mapped;
    
    1296 1296
     
    
    1297
    -   oc->misalignment      = misalignment;
    
    1298 1297
        oc->cxa_finalize      = NULL;
    
    1299 1298
        oc->extraInfos        = NULL;
    
    1300 1299
     
    
    ... ... @@ -1352,7 +1351,6 @@ preloadObjectFile (pathchar *path)
    1352 1351
        int r;
    
    1353 1352
        void *image;
    
    1354 1353
        ObjectCode *oc;
    
    1355
    -   int misalignment = 0;
    
    1356 1354
     
    
    1357 1355
        r = pathstat(path, &st);
    
    1358 1356
        if (r == -1) {
    
    ... ... @@ -1410,29 +1408,8 @@ preloadObjectFile (pathchar *path)
    1410 1408
            return NULL;
    
    1411 1409
        }
    
    1412 1410
     
    
    1413
    -#  if defined(darwin_HOST_OS)
    
    1414
    -
    
    1415
    -    // In a Mach-O .o file, all sections can and will be misaligned
    
    1416
    -    // if the total size of the headers is not a multiple of the
    
    1417
    -    // desired alignment. This is fine for .o files that only serve
    
    1418
    -    // as input for the static linker, but it's not fine for us,
    
    1419
    -    // as SSE (used by gcc for floating point) and Altivec require
    
    1420
    -    // 16-byte alignment.
    
    1421
    -    // We calculate the correct alignment from the header before
    
    1422
    -    // reading the file, and then we misalign image on purpose so
    
    1423
    -    // that the actual sections end up aligned again.
    
    1424
    -   machoGetMisalignment(f, &misalignment);
    
    1425
    -   //machoGetMisalignment might fail to parse the header, but in that
    
    1426
    -   //case so will verifyAndInitOc so we leave cleanup to after verifyAndInitOc.
    
    1427
    -   image = stgMallocBytes(fileSize + misalignment, "loadObj(image)");
    
    1428
    -   image += misalignment;
    
    1429
    -
    
    1430
    -# else /* !defined(darwin_HOST_OS) */
    
    1431
    -
    
    1432 1411
        image = stgMallocBytes(fileSize, "loadObj(image)");
    
    1433 1412
     
    
    1434
    -#endif /* !defined(darwin_HOST_OS) */
    
    1435
    -
    
    1436 1413
        int n;
    
    1437 1414
        n = fread ( image, 1, fileSize, f );
    
    1438 1415
        fclose(f);
    
    ... ... @@ -1447,9 +1424,10 @@ preloadObjectFile (pathchar *path)
    1447 1424
        IF_DEBUG(linker, debugBelch("loadObj: preloaded image at %p\n", (void *) image));
    
    1448 1425
     
    
    1449 1426
        /* FIXME (AP): =mapped= parameter unconditionally set to true */
    
    1450
    -   oc = mkOc(STATIC_OBJECT, path, image, fileSize, true, NULL, misalignment);
    
    1427
    +   oc = mkOc(STATIC_OBJECT, path, image, fileSize, true, NULL);
    
    1451 1428
     
    
    1452 1429
        if (!verifyAndInitOc(oc)) {
    
    1430
    +       freePreloadObjectFile(oc);
    
    1453 1431
            freeObjectCode(oc);
    
    1454 1432
            debugBelch("loadObj: Failed to verify oc.\n");
    
    1455 1433
            return NULL;
    
    ... ... @@ -1510,7 +1488,7 @@ HsInt loadObj (pathchar *path)
    1510 1488
        return r;
    
    1511 1489
     }
    
    1512 1490
     
    
    1513
    -// Call the relevant VeriffyImage_* and ocInit_* functions.
    
    1491
    +// Call the relevant VerifyImage_* and ocInit_* functions.
    
    1514 1492
     // Return 1 on success.
    
    1515 1493
     HsInt verifyAndInitOc (ObjectCode* oc)
    
    1516 1494
     {
    
    ... ... @@ -1536,6 +1514,7 @@ HsInt verifyAndInitOc (ObjectCode* oc)
    1536 1514
     #endif
    
    1537 1515
         if (!r) {
    
    1538 1516
             IF_DEBUG(linker, ocDebugBelch(oc, "ocVerifyImage_* failed\n"));
    
    1517
    +        ocDebugBelch(oc, "ocVerifyImage_* failed\n");
    
    1539 1518
             return r;
    
    1540 1519
         }
    
    1541 1520
         return 1;
    
    ... ... @@ -1571,6 +1550,7 @@ HsInt loadOc (ObjectCode* oc)
    1571 1550
        if (!r) {
    
    1572 1551
            IF_DEBUG(linker,
    
    1573 1552
                     ocDebugBelch(oc, "ocAllocateExtras_MachO failed\n"));
    
    1553
    +       ocDebugBelch(oc, "ocAllocateExtras_MachO failed\n");
    
    1574 1554
            return r;
    
    1575 1555
        }
    
    1576 1556
     #  elif defined(OBJFORMAT_ELF)
    
    ... ... @@ -1595,6 +1575,7 @@ HsInt loadOc (ObjectCode* oc)
    1595 1575
     #  endif
    
    1596 1576
        if (!r) {
    
    1597 1577
            IF_DEBUG(linker, ocDebugBelch(oc, "ocGetNames_* failed\n"));
    
    1578
    +       ocDebugBelch(oc, "ocGetNames_* failed\n");
    
    1598 1579
            return r;
    
    1599 1580
        }
    
    1600 1581
     
    

  • rts/LinkerInternals.h
    ... ... @@ -264,10 +264,6 @@ struct _ObjectCode {
    264 264
         /* non-zero if the object file was mmap'd, otherwise malloc'd */
    
    265 265
         int        imageMapped;
    
    266 266
     
    
    267
    -    /* record by how much image has been deliberately misaligned
    
    268
    -       after allocation, so that we can use realloc */
    
    269
    -    int        misalignment;
    
    270
    -
    
    271 267
         /* The address of __cxa_finalize; set when at least one finalizer was
    
    272 268
          * register and therefore we must call __cxa_finalize before unloading.
    
    273 269
          * See Note [Resolving __dso_handle]. */
    
    ... ... @@ -385,7 +381,7 @@ extern Elf_Word shndx_table_uninit_label;
    385 381
         )
    
    386 382
     
    
    387 383
     #define ocDebugBelch(oc, s, ...) \
    
    388
    -    debugBelch("%s(%" PATH_FMT ": " s, \
    
    384
    +    errorBelch("%s(%" PATH_FMT ": " s, \
    
    389 385
                    __func__, \
    
    390 386
                    OC_INFORMATIVE_FILENAME(oc), \
    
    391 387
                    ##__VA_ARGS__)
    
    ... ... @@ -494,8 +490,7 @@ HsInt verifyAndInitOc( ObjectCode *oc );
    494 490
     //Expects the oc to be verified already.
    
    495 491
     HsInt loadOc( ObjectCode* oc );
    
    496 492
     ObjectCode* mkOc( ObjectType type, pathchar *path, char *image, int imageSize,
    
    497
    -                  bool mapped, pathchar *archiveMemberName,
    
    498
    -                  int misalignment
    
    493
    +                  bool mapped, pathchar *archiveMemberName
    
    499 494
                       );
    
    500 495
     
    
    501 496
     
    

  • rts/linker/LoadArchive.c
    ... ... @@ -26,6 +26,7 @@
    26 26
     #include <ctype.h>
    
    27 27
     #include <sys/types.h>
    
    28 28
     #include <fs_rts.h>
    
    29
    +#include <stdio.h>
    
    29 30
     
    
    30 31
     #define FAIL(...) do {\
    
    31 32
        errorBelch("loadArchive: "__VA_ARGS__); \
    
    ... ... @@ -33,6 +34,7 @@
    33 34
     } while (0)
    
    34 35
     
    
    35 36
     #define DEBUG_LOG(...) IF_DEBUG(linker, debugBelch("loadArchive: " __VA_ARGS__))
    
    37
    +#define DEBUG_LOG_ALWAYS(...) (fprintf(stderr, "loadArchive: " __VA_ARGS__))
    
    36 38
     
    
    37 39
     
    
    38 40
     #if defined(darwin_HOST_OS) || defined(ios_HOST_OS)
    
    ... ... @@ -111,11 +113,8 @@ static bool loadFatArchive(char input[static 20], FILE* f, pathchar* path)
    111 113
     }
    
    112 114
     #endif
    
    113 115
     
    
    114
    -static bool readThinArchiveMember(int memberSize, pathchar* path,
    
    115
    -        char* fileName, char* image)
    
    116
    +static pathchar* thinArchiveMemberPath(pathchar* path, char* fileName)
    
    116 117
     {
    
    117
    -    bool has_succeeded = false;
    
    118
    -    FILE* member = NULL;
    
    119 118
         pathchar *pathCopy, *dirName, *memberPath, *objFileName;
    
    120 119
         memberPath = NULL;
    
    121 120
         /* Allocate and setup the dirname of the archive.  We'll need
    
    ... ... @@ -129,8 +128,22 @@ static bool readThinArchiveMember(int memberSize, pathchar* path,
    129 128
         objFileName = mkPath(fileName);
    
    130 129
         pathprintf(memberPath, memberLen, WSTR("%" PATH_FMT "%" PATH_FMT), dirName,
    
    131 130
                 objFileName);
    
    132
    -    stgFree(objFileName);
    
    131
    +
    
    132
    +    stgFree(pathCopy);
    
    133 133
         stgFree(dirName);
    
    134
    +    stgFree(objFileName);
    
    135
    +    return memberPath;
    
    136
    +}
    
    137
    +
    
    138
    +static bool readThinArchiveMember(int memberSize, pathchar* path,
    
    139
    +        char* fileName, char* image)
    
    140
    +{
    
    141
    +    bool has_succeeded = false;
    
    142
    +    FILE* member = NULL;
    
    143
    +    pathchar *memberPath;
    
    144
    +    //Fully resolved path.
    
    145
    +    memberPath = thinArchiveMemberPath(path, fileName);
    
    146
    +
    
    134 147
         member = pathopen(memberPath, WSTR("rb"));
    
    135 148
         if (!member) {
    
    136 149
             errorBelch("loadObj: can't read thin archive `%" PATH_FMT "'",
    
    ... ... @@ -148,7 +161,6 @@ static bool readThinArchiveMember(int memberSize, pathchar* path,
    148 161
     inner_fail:
    
    149 162
         fclose(member);
    
    150 163
         stgFree(memberPath);
    
    151
    -    stgFree(pathCopy);
    
    152 164
         return has_succeeded;
    
    153 165
     }
    
    154 166
     
    
    ... ... @@ -335,7 +347,7 @@ HsInt loadArchive_ (pathchar *path)
    335 347
         int memberIdx = 0;
    
    336 348
         FILE *f = NULL;
    
    337 349
         size_t thisFileNameSize = (size_t) -1; /* shut up bogus GCC warning */
    
    338
    -    int misalignment = 0;
    
    350
    +    int macho_misalignment = 0;
    
    339 351
     
    
    340 352
         DEBUG_LOG("start\n");
    
    341 353
         DEBUG_LOG("Loading archive `%" PATH_FMT "'\n", path);
    
    ... ... @@ -344,7 +356,7 @@ HsInt loadArchive_ (pathchar *path)
    344 356
            Ignore requests to load multiple times */
    
    345 357
         if (isAlreadyLoaded(path)) {
    
    346 358
             IF_DEBUG(linker,
    
    347
    -                 debugBelch("ignoring repeated load of %" PATH_FMT "\n", path));
    
    359
    +                 DEBUG_LOG("ignoring repeated load of %" PATH_FMT "\n", path));
    
    348 360
             return 1; /* success */
    
    349 361
         }
    
    350 362
     
    
    ... ... @@ -365,6 +377,7 @@ HsInt loadArchive_ (pathchar *path)
    365 377
         bool isThin = archive_fmt == ThinArchive;
    
    366 378
         if(isThin) {
    
    367 379
             DEBUG_LOG("Found thin archive.\n");
    
    380
    +        DEBUG_LOG_ALWAYS("Found thin archive.\n");
    
    368 381
         }
    
    369 382
     
    
    370 383
         DEBUG_LOG("loading archive contents\n");
    
    ... ... @@ -507,6 +520,17 @@ HsInt loadArchive_ (pathchar *path)
    507 520
     
    
    508 521
             DEBUG_LOG("Found member file `%s'\n", fileName);
    
    509 522
             bool is_symbol_table = strcmp("", fileName) == 0;
    
    523
    +#if defined(OBJFORMAT_MACHO)
    
    524
    +        if (!is_symbol_table) {
    
    525
    +            /* Darwin ranlib symbol tables are named __.SYMDEF* */
    
    526
    +            if (strncmp(fileName, "__.SYMDEF", sizeof("__.SYMDEF")) == 0 ||
    
    527
    +                strncmp(fileName, "__.SYMDEF SORTED", sizeof("__.SYMDEF SORTED")) == 0 ||
    
    528
    +                strncmp(fileName, "__.SYMDEF_64", sizeof("__.SYMDEF_64")) == 0 ||
    
    529
    +                strncmp(fileName, "__.SYMDEF_64 SORTED", sizeof("__.SYMDEF_64 SORTED")) == 0) {
    
    530
    +                is_symbol_table = true;
    
    531
    +            }
    
    532
    +        }
    
    533
    +#endif
    
    510 534
     
    
    511 535
     /////////////////////////////////////////////////
    
    512 536
     // We found the member file. Load it into memory.
    
    ... ... @@ -571,27 +595,21 @@ HsInt loadArchive_ (pathchar *path)
    571 595
     #endif
    
    572 596
             )
    
    573 597
             {
    
    598
    +            ASSERT(!isGnuIndex);
    
    574 599
                 DEBUG_LOG("Member might be an object file...loading...\n");
    
    575 600
     #if defined(darwin_HOST_OS) || defined(ios_HOST_OS)
    
    576
    -            if (RTS_LINKER_USE_MMAP)
    
    577
    -                image = mmapAnonForLinker(memberSize);
    
    578
    -            else {
    
    579
    -                /* See loadObj() */
    
    580
    -                if(!machoGetMisalignment(f, &misalignment)) {
    
    581
    -                    DEBUG_LOG("Failed to load member as mach-o file. Skipping.\n");
    
    582
    -                    continue;
    
    583
    -                }
    
    584
    -                image = stgMallocBytes(memberSize + misalignment,
    
    585
    -                                        "loadArchive(image)");
    
    586
    -                image += misalignment;
    
    587
    -            }
    
    601
    +#if defined(RTS_LINKER_USE_MMAP)
    
    602
    +            image = mmapAnonForLinker(memberSize);
    
    603
    +#else
    
    604
    +#error "Only MMAP based loading supported on Apple platforms"
    
    605
    +#endif // defined(RTS_LINKER_USE_MMAP)
    
    588 606
     
    
    589 607
     #else // not darwin
    
    590 608
                 image = stgMallocBytes(memberSize, "loadArchive(image)");
    
    591 609
     #endif
    
    592 610
                 if (isThin) {
    
    593 611
                     if (!readThinArchiveMember(memberSize, path, fileName, image)) {
    
    594
    -                    goto fail;
    
    612
    +                    FAIL("Failed to read thin member %" PATH_FMT"\n", path);
    
    595 613
                     }
    
    596 614
                 }
    
    597 615
                 else
    
    ... ... @@ -616,20 +634,23 @@ HsInt loadArchive_ (pathchar *path)
    616 634
     ///////////////////////////////////////////////////////////////
    
    617 635
     
    
    618 636
                 // Prepare headers, doesn't load any data yet.
    
    619
    -            ObjectCode *oc = mkOc(STATIC_OBJECT, path, image, memberSize, false, archiveMemberName,
    
    620
    -                                  misalignment);
    
    637
    +            ObjectCode *oc = mkOc(STATIC_OBJECT, path, image, memberSize, false, archiveMemberName);
    
    621 638
                 stgFree(archiveMemberName);
    
    622 639
     
    
    623 640
                 if(!verifyAndInitOc( oc ))
    
    624 641
                 {
    
    625
    -                freeObjectCode( oc );
    
    626 642
                     IF_DEBUG(linker, ocDebugBelch(oc, "Faild to verify ... skipping."));
    
    643
    +                errorBelch("Faild to verify ... skipping.");
    
    644
    +                freeObjectCode( oc );
    
    645
    +                //TODO: Unmap or free image
    
    627 646
                     continue;
    
    628 647
                 }
    
    629 648
     
    
    630 649
     
    
    631 650
                 if (0 == loadOc(oc)) {
    
    651
    +                errorBelch("Failed to load OC %" PATH_FMT " , aborting.\n", path);
    
    632 652
                     stgFree(fileName);
    
    653
    +                freeObjectCode( oc );
    
    633 654
                     fclose(f);
    
    634 655
                     return 0;
    
    635 656
                 } else {
    
    ... ... @@ -744,4 +765,3 @@ bool isArchive (pathchar *path)
    744 765
         }
    
    745 766
         return strncmp(ARCHIVE_HEADER, buffer, sizeof(ARCHIVE_HEADER)-1) == 0;
    
    746 767
     }
    747
    -

  • rts/linker/LoadNativeObjPosix.c
    ... ... @@ -134,7 +134,7 @@ void * loadNativeObj_POSIX (pathchar *path, char **errmsg)
    134 134
          goto dlopen_fail;
    
    135 135
        }
    
    136 136
     
    
    137
    -   nc = mkOc(DYNAMIC_OBJECT, path, NULL, 0, false, NULL, 0);
    
    137
    +   nc = mkOc(DYNAMIC_OBJECT, path, NULL, 0, false, NULL);
    
    138 138
     
    
    139 139
        // If we HAVE_DLINFO, we use RTLD_NOW rather than RTLD_LAZY because we want
    
    140 140
        // to learn eagerly about all external functions. Otherwise, there is no
    

  • rts/linker/MachO.c
    1 1
     #include "Rts.h"
    
    2
    +#include "rts/Linker.h"
    
    2 3
     
    
    3 4
     #if defined(darwin_HOST_OS) || defined(ios_HOST_OS)
    
    4 5
     
    
    ... ... @@ -200,6 +201,10 @@ int
    200 201
     ocVerifyImage_MachO(ObjectCode * oc)
    
    201 202
     {
    
    202 203
         char *image = (char*) oc->image;
    
    204
    +    if (oc->fileSize < (int) sizeof(MachOHeader)) {
    
    205
    +        errorBelch("Tried loading machO smaller than header size.\n");
    
    206
    +        return 0;
    
    207
    +    }
    
    203 208
         MachOHeader *header = (MachOHeader*) image;
    
    204 209
     
    
    205 210
         IF_DEBUG(linker, debugBelch("ocVerifyImage_MachO: start\n"));
    
    ... ... @@ -1774,44 +1779,4 @@ ocRunFini_MachO ( ObjectCode *oc )
    1774 1779
         return 1;
    
    1775 1780
     }
    
    1776 1781
     
    
    1777
    -/*
    
    1778
    - * Figure out by how much to shift the entire Mach-O file in memory
    
    1779
    - * when loading so that its single segment ends up 16-byte-aligned
    
    1780
    - *
    
    1781
    - * Returns 1 and sets misalignment_out to the detected misalignment if
    
    1782
    - * we successfully parsed the file.
    
    1783
    - *
    
    1784
    - * If we can't parse the file we set misalignment_out to 0 and return 0
    
    1785
    - */
    
    1786
    -int
    
    1787
    -machoGetMisalignment( FILE * f, int* misalignment_out )
    
    1788
    -{
    
    1789
    -    MachOHeader header;
    
    1790
    -    int misalignment;
    
    1791
    -    *misalignment_out = 0;
    
    1792
    -
    
    1793
    -    {
    
    1794
    -        size_t n = fread(&header, sizeof(header), 1, f);
    
    1795
    -        if (n != 1) {
    
    1796
    -            debugBelch("machoGetMisalignment: can't read the Mach-O header");
    
    1797
    -            return 0;
    
    1798
    -        }
    
    1799
    -    }
    
    1800
    -    fseek(f, -sizeof(header), SEEK_CUR);
    
    1801
    -
    
    1802
    -    if(header.magic != MH_MAGIC_64) {
    
    1803
    -        debugBelch("Bad magic. Expected: %08x, got: %08x.",
    
    1804
    -             MH_MAGIC_64, header.magic);
    
    1805
    -        return 0;
    
    1806
    -    }
    
    1807
    -
    
    1808
    -    misalignment = (header.sizeofcmds + sizeof(header))
    
    1809
    -                    & 0xF;
    
    1810
    -
    
    1811
    -    IF_DEBUG(linker, debugBelch("mach-o misalignment %d\n", misalignment));
    
    1812
    -    misalignment = misalignment ? (16 - misalignment) : 0;
    
    1813
    -    *misalignment_out = misalignment;
    
    1814
    -    return 1;
    
    1815
    -}
    
    1816
    -
    
    1817 1782
     #endif /* darwin_HOST_OS || ios_HOST_OS */

  • rts/linker/MachO.h
    ... ... @@ -13,7 +13,6 @@ int ocGetNames_MachO ( ObjectCode* oc );
    13 13
     int    ocResolve_MachO              ( ObjectCode* oc );
    
    14 14
     int    ocRunInit_MachO              ( ObjectCode* oc );
    
    15 15
     int    ocRunFini_MachO              ( ObjectCode* oc );
    
    16
    -int    machoGetMisalignment         ( FILE *, int* );
    
    17 16
     int    ocAllocateExtras_MachO       ( ObjectCode* oc );
    
    18 17
     
    
    19 18
     SectionKind getSectionKind_MachO    ( MachOSection *macho );
    

  • rts/linker/SymbolExtras.c
    ... ... @@ -63,14 +63,10 @@ int ocAllocateExtras(ObjectCode* oc, int count, int first, int bssSize)
    63 63
     
    
    64 64
           // round up to the nearest 4
    
    65 65
           int aligned = (oc->fileSize + 3) & ~3;
    
    66
    -      int misalignment = oc->misalignment;
    
    67 66
     
    
    68
    -      oc->image -= misalignment;
    
    69 67
           oc->image = stgReallocBytes( oc->image,
    
    70
    -                               misalignment +
    
    71 68
                                    aligned + extras_size,
    
    72 69
                                    "ocAllocateExtras" );
    
    73
    -      oc->image += misalignment;
    
    74 70
     
    
    75 71
           oc->symbol_extras = (SymbolExtra *) (oc->image + aligned);
    
    76 72
         } else if (USE_CONTIGUOUS_MMAP || RtsFlags.MiscFlags.linkerAlwaysPic) {