diff --git a/src/link/sdas_obj.cpp b/src/link/sdas_obj.cpp index 96d7968b..928c5a69 100644 --- a/src/link/sdas_obj.cpp +++ b/src/link/sdas_obj.cpp @@ -234,7 +234,6 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector fileSections; - size_t nbSections = 0, nbSymbols = 0; size_t nbBytes = 0; // How many bytes are in `data`, including the ADDR_SIZE "header" bytes size_t dataCapacity = 16 + ADDR_SIZE; // SDCC object files usually contain 16 bytes per T line uint8_t *data = (uint8_t *)malloc(sizeof(*data) * dataCapacity); @@ -253,27 +252,26 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vectorname->c_str())) + for (struct FileSection &entry : fileSections) { + if (!strcmp(token, entry.section->name->c_str())) fatal(where, lineNo, "Area \"%s\" already defined earlier", token); } char const *sectionName = token; // We'll deal with the section's name depending on type + fileSections.push_back({ .section = curSection, .writeIndex = 0 }); + expectToken("size", 'A'); getToken(NULL, "'A' line is too short"); @@ -355,13 +353,11 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vectornextu = NULL; - - ++nbSections; break; } case 'S': { - if (nbSymbols == expectedNbSymbols) + if (fileSymbols.size() == expectedNbSymbols) warning(where, lineNo, "Got more 'S' lines than the expected %" PRIu32, expectedNbSymbols); struct Symbol &symbol = fileSymbols.emplace_back(); @@ -372,7 +368,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vectorsymbols->push_back(&symbol); + if (!fileSections.empty()) + fileSections.back().section->symbols->push_back(&symbol); expectEol("'S' line is too long"); - - ++nbSymbols; break; } @@ -468,9 +462,9 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector= nbSections) + if (areaIdx >= fileSections.size()) fatal(where, lineNo, "'R' line references area #%" PRIu16 ", but there are only %zu (so far)", - areaIdx, nbSections); + areaIdx, fileSections.size()); assert(!fileSections.empty()); // There should be at least one, from the above check struct Section *section = fileSections[areaIdx].section; uint16_t *writeIndex = &fileSections[areaIdx].writeIndex; @@ -570,9 +564,9 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector= nbSymbols) + if (idx >= fileSymbols.size()) fatal(where, lineNo, "Reloc refers to symbol #%" PRIu16 " out of %zu", - idx, nbSymbols); + idx, fileSymbols.size()); struct Symbol const &sym = fileSymbols[idx]; // SDCC has a bunch of "magic symbols" that start with a @@ -580,12 +574,12 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vectorstarts_with("b_")) { // Look for the symbol being referenced, and use its index instead - for (idx = 0; idx < nbSymbols; ++idx) { + for (idx = 0; idx < fileSymbols.size(); ++idx) { if (sym.name->ends_with(*fileSymbols[idx].name) && 1 + sym.name->length() == fileSymbols[idx].name->length()) break; } - if (idx == nbSymbols) + if (idx == fileSymbols.size()) fatal(where, lineNo, "\"%s\" is missing a reference to \"%s\"", sym.name->c_str(), &sym.name->c_str()[1]); patch.rpnExpression.resize(5); @@ -611,9 +605,9 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector> 24; } } else { - if (idx >= nbSections) + if (idx >= fileSections.size()) fatal(where, lineNo, "Reloc refers to area #%" PRIu16 " out of %zu", - idx, nbSections); + idx, fileSections.size()); // It gets funky. If the area is absolute, *actually*, we // must not add its base address, as the assembler will // already have added it in `baseValue`. @@ -722,20 +716,22 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vectorsize && fileSections[i].writeIndex != 0) + if (entry.writeIndex != section->size && entry.writeIndex != 0) fatal(where, lineNo, "\"%s\" was not fully written (%" PRIu16 " < %" PRIu16 ")", - section->name->c_str(), fileSections[i].writeIndex, section->size); + section->name->c_str(), entry.writeIndex, section->size); // This must be done last, so that `->data` is not NULL anymore sect_AddSection(section);