[PATCH 3/4] For DRI style symbol tables, try to auto-detect whether DATA/BSS addresses need to be offset by the section start

[ Thread Index | Date Index | More lists.tuxfamily.org/hatari-devel Archives ]


---
 src/debug/symbols-common.c | 63 ++++++++++++++++++++++++++++++--------
 1 file changed, 50 insertions(+), 13 deletions(-)

diff --git a/src/debug/symbols-common.c b/src/debug/symbols-common.c
index ed6ae35b..4ce1a4c0 100644
--- a/src/debug/symbols-common.c
+++ b/src/debug/symbols-common.c
@@ -1,11 +1,11 @@
 /*
  * Hatari - symbols-common.c
- * 
+ *
  * Copyright (C) 2010-2019 by Eero Tamminen
- * 
+ *
  * This file is distributed under the GNU General Public License, version 2
  * or at your option any later version. Read the file gpl.txt for details.
- * 
+ *
  * symbols-common.c - Hatari debugger symbol/address handling; parsing, sorting,
  * matching, TAB completion support etc.
  * This code is shared between the internal debug "symbols" command
@@ -274,6 +274,8 @@ static symbol_list_t* symbols_load_dri(FILE *fp, prg_section_t *sections, symtyp
 	char name[23];
 	uint16_t symid;
 	uint32_t address;
+	bool offset_addresses;
+	uint32_t textlen = sections[0].end - sections[0].offset;
 
 	if (tablesize % DRI_ENTRY_SIZE || !tablesize) {
 		fprintf(stderr, "ERROR: invalid DRI/GST symbol table size %d!\n", tablesize);
@@ -285,6 +287,7 @@ static symbol_list_t* symbols_load_dri(FILE *fp, prg_section_t *sections, symtyp
 	}
 
 	invalid = dtypes = notypes = ofiles = locals = count = 0;
+	offset_addresses = false;
 	for (i = 1; i <= symbols; i++) {
 		/* read DRI symbol table slot */
 		if (fread(name, 8, 1, fp) != 1 ||
@@ -316,10 +319,14 @@ static symbol_list_t* symbols_load_dri(FILE *fp, prg_section_t *sections, symtyp
 		case 0x0400:
 			symtype = SYMTYPE_DATA;
 			section = &(sections[1]);
+			if (address < textlen)
+				offset_addresses = true;
 			break;
 		case 0x0100:
 			symtype = SYMTYPE_BSS;
 			section = &(sections[2]);
+			if (address < textlen)
+				offset_addresses = true;
 			break;
 		default:
 			if ((symid & 0xe000) == 0xe000) {
@@ -351,15 +358,6 @@ static symbol_list_t* symbols_load_dri(FILE *fp, prg_section_t *sections, symtyp
 				continue;
 			}
 		}
-		if (section) {
-			address += section->offset;
-			if (address > section->end) {
-				fprintf(stderr, "WARNING: ignoring symbol '%s' of type %c in slot %d with invalid offset 0x%x (>= 0x%x).\n",
-					name, symbol_char(symtype), i, address, section->end);
-				invalid++;
-				continue;
-			}
-		}
 		list->names[count].address = address;
 		list->names[count].type = symtype;
 		list->names[count].name = strdup(name);
@@ -371,9 +369,48 @@ static symbol_list_t* symbols_load_dri(FILE *fp, prg_section_t *sections, symtyp
 		symbol_list_free(list);
 		return NULL;
 	}
+
 	list->symbols = symbols;
 	list->namecount = count;
 
+	/*
+	 * now offset the addresses if needed, and check them
+	 */
+	count = 0;
+	for (i = 0; i < list->namecount; i++) {
+		switch (list->names[i].type) {
+		case SYMTYPE_TEXT:
+			section = &(sections[0]);
+			break;
+		case SYMTYPE_DATA:
+			section = &(sections[1]);
+			break;
+		case SYMTYPE_BSS:
+			section = &(sections[2]);
+			break;
+		default:
+			section = NULL;
+			break;
+		}
+		if (section) {
+			if (offset_addresses)
+				list->names[i].address += section->offset;
+			if (list->names[i].address > section->end) {
+				fprintf(stderr, "WARNING: ignoring symbol '%s' of type %c in slot %d with invalid offset 0x%x (>= 0x%x).\n",
+					name, symbol_char(symtype), i, list->names[i].address, section->end);
+				invalid++;
+				free(list->names[i].name);
+				continue;
+			}
+		}
+		list->names[count] = list->names[i];
+		count++;
+	}
+	/*
+	 * update new final count again
+	 */
+	list->namecount = count;
+
 	if (invalid) {
 		fprintf(stderr, "NOTE: ignored %d invalid symbols.\n", invalid);
 	}
@@ -690,7 +727,7 @@ static symbol_list_t* symbols_load_binary(FILE *fp, symtype_t gettype)
 	prgflags = SDL_SwapBE32(prgflags);
 	reads += fread(&relocflag, sizeof(relocflag), 1, fp);
 	relocflag = SDL_SwapBE32(relocflag);
-	
+
 	if (reads != 7) {
 		fprintf(stderr, "ERROR: program header reading failed!\n");
 		return NULL;
-- 
2.24.0


--nextPart3945085.DFmjOQq6lO
Content-Disposition: attachment; filename="0004-Add-support-to-read-long-symbol-names-from-Pure-C-de.patch"
Content-Transfer-Encoding: 7Bit
Content-Type: text/x-patch; charset="UTF-8"; name="0004-Add-support-to-read-long-symbol-names-from-Pure-C-de.patch"



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/