Andreas Klebinger pushed to branch wip/andreask/linker_fix at Glasgow Haskell Compiler / GHC
Commits:
-
2c39a9a3
by Andreas Klebinger at 2026-01-29T13:32:00+01:00
4 changed files:
Changes:
| ... | ... | @@ -1415,7 +1415,7 @@ preloadObjectFile (pathchar *path) |
| 1415 | 1415 | // We calculate the correct alignment from the header before
|
| 1416 | 1416 | // reading the file, and then we misalign image on purpose so
|
| 1417 | 1417 | // that the actual sections end up aligned again.
|
| 1418 | - machoGetMisalignment(f, &misalignment);
|
|
| 1418 | + int valid_macho = machoGetMisalignment(f, &misalignment);
|
|
| 1419 | 1419 | //machoGetMisalignment might fail to parse the header, but in that
|
| 1420 | 1420 | //case so will verifyAndInitOc so we leave cleanup to after verifyAndInitOc.
|
| 1421 | 1421 | image = stgMallocBytes(fileSize + misalignment, "loadObj(image)");
|
| ... | ... | @@ -1504,7 +1504,7 @@ HsInt loadObj (pathchar *path) |
| 1504 | 1504 | return r;
|
| 1505 | 1505 | }
|
| 1506 | 1506 | |
| 1507 | -// Call the relevant VeriffyImage_* and ocInit_* functions.
|
|
| 1507 | +// Call the relevant VerifyImage_* and ocInit_* functions.
|
|
| 1508 | 1508 | // Return 1 on success.
|
| 1509 | 1509 | HsInt verifyAndInitOc (ObjectCode* oc)
|
| 1510 | 1510 | {
|
| ... | ... | @@ -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)
|
| ... | ... | @@ -344,7 +346,7 @@ HsInt loadArchive_ (pathchar *path) |
| 344 | 346 | Ignore requests to load multiple times */
|
| 345 | 347 | if (isAlreadyLoaded(path)) {
|
| 346 | 348 | IF_DEBUG(linker,
|
| 347 | - debugBelch("ignoring repeated load of %" PATH_FMT "\n", path));
|
|
| 349 | + DEBUG_LOG("ignoring repeated load of %" PATH_FMT "\n", path));
|
|
| 348 | 350 | return 1; /* success */
|
| 349 | 351 | }
|
| 350 | 352 | |
| ... | ... | @@ -365,6 +367,7 @@ HsInt loadArchive_ (pathchar *path) |
| 365 | 367 | bool isThin = archive_fmt == ThinArchive;
|
| 366 | 368 | if(isThin) {
|
| 367 | 369 | DEBUG_LOG("Found thin archive.\n");
|
| 370 | + DEBUG_LOG_ALWAYS("Found thin archive.\n");
|
|
| 368 | 371 | }
|
| 369 | 372 | |
| 370 | 373 | DEBUG_LOG("loading archive contents\n");
|
| ... | ... | @@ -630,6 +633,7 @@ HsInt loadArchive_ (pathchar *path) |
| 630 | 633 | |
| 631 | 634 | |
| 632 | 635 | if (0 == loadOc(oc)) {
|
| 636 | + errorBelch("Failed to load OC %" PATH_FMT " , aborting.\n", path);
|
|
| 633 | 637 | stgFree(fileName);
|
| 634 | 638 | fclose(f);
|
| 635 | 639 | return 0;
|
| ... | ... | @@ -175,7 +175,7 @@ resolveImports( |
| 175 | 175 | int
|
| 176 | 176 | ocAllocateExtras_MachO(ObjectCode* oc)
|
| 177 | 177 | {
|
| 178 | - IF_DEBUG(linker, debugBelch("ocAllocateExtras_MachO: start\n"));
|
|
| 178 | + IF_DEBUG(linker, DEBUG_LOG_ALWAYS("ocAllocateExtras_MachO: start\n"));
|
|
| 179 | 179 | |
| 180 | 180 | if (NULL != oc->info->symCmd) {
|
| 181 | 181 | IF_DEBUG(linker,
|
| ... | ... | @@ -200,6 +200,10 @@ int |
| 200 | 200 | ocVerifyImage_MachO(ObjectCode * oc)
|
| 201 | 201 | {
|
| 202 | 202 | char *image = (char*) oc->image;
|
| 203 | + if (oc->fileSize < sizeof(MachOHeader)) {
|
|
| 204 | + errorBelch("Tried loading machO smaller than header size.\n");
|
|
| 205 | + return 0;
|
|
| 206 | + }
|
|
| 203 | 207 | MachOHeader *header = (MachOHeader*) image;
|
| 204 | 208 | |
| 205 | 209 | IF_DEBUG(linker, debugBelch("ocVerifyImage_MachO: start\n"));
|
| ... | ... | @@ -475,7 +475,7 @@ static void addDLLHandle( |
| 475 | 475 | |
| 476 | 476 | static bool verifyCOFFHeader(
|
| 477 | 477 | uint16_t machine,
|
| 478 | - IMAGE_FILE_HEADER *hdr
|
|
| 478 | + IMAGE_FILE_HEADER *hdr,
|
|
| 479 | 479 | pathchar *fileName);
|
| 480 | 480 | |
| 481 | 481 | static bool checkIfDllLoaded(
|