]> git.sesse.net Git - pistorm/blobdiff - platforms/amiga/hunk-reloc.c
Some PiSCSI file system reloc changes
[pistorm] / platforms / amiga / hunk-reloc.c
index b19c900b95c1e4d3ec9688470d641e98fea6b542..e7c296dbbfa7896c646e0de5c4777303e99e77a4 100644 (file)
@@ -14,6 +14,8 @@
 #define lseek64 lseek
 #endif
 
+#define DEBUG_SPAMMY(...)
+//#define DEBUG_SPAMMY printf
 #define DEBUG(...)
 //#define DEBUG printf
 
@@ -55,10 +57,14 @@ int process_hunk(uint32_t index, struct hunk_info *info, FILE *f, struct hunk_re
     
     switch (index) {
         case HUNKTYPE_HEADER:
-            DEBUG("Processing hunk header.\n");
+            DEBUG("[HUNK_RELOC] Processing hunk HEADER.\n");
             do {
                 READLW(discard, f);
                 if (discard) {
+                    if (info->libnames[info->num_libs]) {
+                        free(info->libnames[info->num_libs]);
+                        info->libnames[info->num_libs] = NULL;
+                    }
                     info->libnames[info->num_libs] = malloc(discard * 4);
                     fread(info->libnames[info->num_libs], discard, 4, f);
                     info->num_libs++;
@@ -66,38 +72,48 @@ int process_hunk(uint32_t index, struct hunk_info *info, FILE *f, struct hunk_re
             } while (discard);
             
             READLW(info->table_size, f);
-            DEBUG("Table size: %d\n", info->table_size);
+            DEBUG("[HUNK_RELOC] [HEADER] Table size: %d\n", info->table_size);
             READLW(info->first_hunk, f);
             READLW(info->last_hunk, f);
             info->num_hunks = (info->last_hunk - info->first_hunk) + 1;
-            DEBUG("First: %d Last: %d Num: %d\n", info->first_hunk, info->last_hunk, info->num_hunks);
+            DEBUG("[HUNK_RELOC] [HEADER] First: %d Last: %d Num: %d\n", info->first_hunk, info->last_hunk, info->num_hunks);
+            if (info->hunk_sizes) {
+                free(info->hunk_sizes);
+                info->hunk_sizes = NULL;
+            }
+            if (info->hunk_offsets) {
+                free(info->hunk_offsets);
+                info->hunk_offsets = NULL;
+            }
             info->hunk_sizes = malloc(info->num_hunks * 4);
             info->hunk_offsets = malloc(info->num_hunks * 4);
             for (uint32_t i = 0; i < info->table_size; i++) {
                 READLW(info->hunk_sizes[i], f);
-                DEBUG("Hunk %d: %d (%.8X)\n", i, info->hunk_sizes[i] * 4, info->hunk_sizes[i] * 4);
+                DEBUG("[HUNK_RELOC] [HEADER] Hunk %d: %d (%.8X)\n", i, info->hunk_sizes[i] * 4, info->hunk_sizes[i] * 4);
             }
+            info->header_size = (uint32_t)ftell(f) - file_offset;
+            DEBUG("[HUNK_RELOC] [HEADER] ~~~~~~~~~~~ Hunk HEADER size is %d ~~~~~~~~~~~~.\n", info->header_size);
             return 0;
             break;
         case HUNKTYPE_CODE:
-            DEBUG("Hunk %d: CODE.\n", info->current_hunk);
+            DEBUG("[HUNK_RELOC] Hunk %d: CODE.\n", info->current_hunk);
             READLW(discard, f);
             info->hunk_offsets[info->current_hunk] = ftell(f) - file_offset;
-            DEBUG("Code hunk size: %d (%.8X)\n", discard * 4, discard * 4);
+            DEBUG("[HUNK_RELOC] [CODE] Code hunk size: %d (%.8X)\n", discard * 4, discard * 4);
             fseek(f, discard * 4, SEEK_CUR);
             return 0;
             break;
         case HUNKTYPE_HUNK_RELOC32:
-            DEBUG("Hunk %d: RELOC32.\n", info->current_hunk);
+            DEBUG("[HUNK_RELOC] Hunk %d: RELOC32.\n", info->current_hunk);
             DEBUG("Processing Reloc32 hunk.\n");
             do {
                 READLW(discard, f);
                 if (discard && discard != 0xFFFFFFFF) {
                     READLW(cur_hunk, f);
-                    DEBUG("Relocating %d offsets pointing to hunk %d.\n", discard, cur_hunk);
+                    DEBUG("[HUNK_RELOC] [RELOC32] Relocating %d offsets pointing to hunk %d.\n", discard, cur_hunk);
                     for(uint32_t i = 0; i < discard; i++) {
                         READLW(offs32, f);
-                        DEBUG("#%d: @%.8X in hunk %d\n", i + 1, offs32, cur_hunk);
+                        DEBUG_SPAMMY("[HUNK_RELOC] [RELOC32] #%d: @%.8X in hunk %d\n", i + 1, offs32, cur_hunk);
                         r[info->reloc_hunks].offset = offs32;
                         r[info->reloc_hunks].src_hunk = info->current_hunk;
                         r[info->reloc_hunks].target_hunk = cur_hunk;
@@ -108,8 +124,8 @@ int process_hunk(uint32_t index, struct hunk_info *info, FILE *f, struct hunk_re
             return 0;
             break;
         case HUNKTYPE_SYMBOL:
-            DEBUG("Hunk %d: SYMBOL.\n", info->current_hunk);
-            DEBUG("Processing Symbol hunk.\n");
+            DEBUG("[HUNK_RELOC] Hunk %d: SYMBOL.\n", info->current_hunk);
+            DEBUG("[HUNK_RELOC] [SYMBOL] Processing Symbol hunk.\n");
             READLW(discard, f);
             do {
                 if (discard) {
@@ -117,34 +133,34 @@ int process_hunk(uint32_t index, struct hunk_info *info, FILE *f, struct hunk_re
                     memset(sstr, 0x00, 256);
                     fread(sstr, discard, 4, f);
                     READLW(discard, f);
-                    DEBUG("Symbol: %s - %.8X\n", sstr, discard);
+                    DEBUG("[HUNK_RELOC] [SYMBOL] Symbol: %s - %.8X\n", sstr, discard);
                 }
                 READLW(discard, f);
             } while (discard);
             return 0;
             break;
         case HUNKTYPE_BSS:
-            DEBUG("Hunk %d: BSS.\n", info->current_hunk);
+            DEBUG("[HUNK_RELOC] Hunk %d: BSS.\n", info->current_hunk);
             READLW(discard, f);
             info->hunk_offsets[info->current_hunk] = ftell(f) - file_offset;
-            DEBUG("Skipping BSS hunk. Size: %d\n", discard * 4);
+            DEBUG("[HUNK_RELOC] [BSS] Skipping BSS hunk. Size: %d\n", discard * 4);
             add_size += (discard * 4);
             return 0;
         case HUNKTYPE_DATA:
-            DEBUG("Hunk %d: DATA.\n", info->current_hunk);
+            DEBUG("[HUNK_RELOC] Hunk %d: DATA.\n", info->current_hunk);
             READLW(discard, f);
             info->hunk_offsets[info->current_hunk] = ftell(f) - file_offset;
-            DEBUG("Skipping data hunk. Size: %d.\n", discard * 4);
+            DEBUG("[HUNK_RELOC] [DATA] Skipping data hunk. Size: %d.\n", discard * 4);
             fseek(f, discard * 4, SEEK_CUR);
             return 0;
             break;
         case HUNKTYPE_END:
-            DEBUG("END: Ending hunk %d.\n", info->current_hunk);
+            DEBUG("[HUNK_RELOC] END: Ending hunk %d.\n", info->current_hunk);
             info->current_hunk++;
             return 0;
             break;
         default:
-            DEBUG("Unknown hunk type %.8X! Can't process!\n", index);
+            DEBUG("[!!!HUNK_RELOC] Unknown hunk type %.8X! Can't process!\n", index);
             break;
     }
 
@@ -157,13 +173,13 @@ void reloc_hunk(struct hunk_reloc *h, uint8_t *buf, struct hunk_info *i) {
 
     uint32_t src = be32toh(*src_ptr);
     uint32_t dst = src + i->base_offset + rel;
-    DEBUG("%.8X -> %.8X\n", src, dst);
+    DEBUG_SPAMMY("[HUNK-RELOC] %.8X -> %.8X\n", src, dst);
     *src_ptr = htobe32(dst);
 }
 
 void process_hunks(FILE *in, struct hunk_info *h_info, struct hunk_reloc *r, uint32_t offset) {
     READLW(lw, in);
-    DEBUG("Hunk ID: %.8X (%s)\n", lw, hunk_id_name(lw));
+    DEBUG_SPAMMY("Hunk ID: %.8X (%s)\n", lw, hunk_id_name(lw));
 
     file_offset = offset;
     add_size = 0;
@@ -179,12 +195,12 @@ void process_hunks(FILE *in, struct hunk_info *h_info, struct hunk_reloc *r, uin
 }
 
 void reloc_hunks(struct hunk_reloc *r, uint8_t *buf, struct hunk_info *h_info) {
-    DEBUG("Relocating %d offsets.\n", h_info->reloc_hunks);
+    DEBUG("[HUNK-RELOC] Relocating %d offsets.\n", h_info->reloc_hunks);
     for (uint32_t i = 0; i < h_info->reloc_hunks; i++) {
-        DEBUG("Relocating offset %d.\n", i);
+        DEBUG_SPAMMY("[HUNK-RELOC] Relocating offset %d.\n", i);
         reloc_hunk(&r[i], buf, h_info);
     }
-    DEBUG("Done relocating offsets.\n");
+    DEBUG("[HUNK-RELOC] Done relocating offsets.\n");
 }
 
 struct LoadSegBlock {