Skip to content

Commit

Permalink
Finish linker script implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ISSOtm committed Dec 10, 2023
1 parent 8601ab6 commit a664c1d
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 97 deletions.
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ rgblink_obj := \
src/error.o \
src/hashmap.o \
src/linkdefs.o \
src/opmath.o
src/opmath.o \
src/util.o

rgbfix_obj := \
src/fix/main.o \
Expand Down Expand Up @@ -139,8 +140,6 @@ test/gfx/rgbgfx_test: test/gfx/rgbgfx_test.cpp
# Bison-generated C++ files have an accompanying header
src/asm/parser.hpp: src/asm/parser.cpp
$Qtouch $@
src/link/script.hpp: src/link/script.cpp
$Qtouch $@

# Only RGBGFX uses libpng (POSIX make doesn't support pattern rules to cover all these)
src/gfx/main.o: src/gfx/main.cpp
Expand Down
21 changes: 2 additions & 19 deletions include/link/script.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,9 @@

#include "linkdefs.hpp"

extern FILE * linkerScript;

struct SectionPlacement {
struct Section *section;
enum SectionType type;
uint16_t org;
uint32_t bank;
};

extern uint64_t script_lineNo;

/*
* Parses the linker script to return the next section constraint
* @return A pointer to a struct, or NULL on EOF. The pointer shouldn't be freed
*/
struct SectionPlacement *script_NextSection(void);

/*
* `free`s all assignment memory that was allocated.
* Parses the linker script, and modifies the sections mentioned within appropriately.
*/
void script_Cleanup(void);
void script_ProcessScript(char const *path);

#endif // RGBDS_LINK_SCRIPT_H
11 changes: 6 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ endif()
set(BISON_FLAGS "${BISON_FLAGS} -Dparse.lac=full")
set(BISON_FLAGS "${BISON_FLAGS} -Dlr.type=ielr")

BISON_TARGET(PARSER "asm/parser.y"
BISON_TARGET(ASM_PARSER "asm/parser.y"
"${PROJECT_SOURCE_DIR}/src/asm/parser.cpp"
COMPILE_FLAGS "${BISON_FLAGS}"
DEFINES_FILE "${PROJECT_SOURCE_DIR}/src/asm/parser.hpp"
)

BISON_TARGET(PARSER "link/script.y"
BISON_TARGET(LINKER_SCRIPT_PARSER "link/script.y"
"${PROJECT_SOURCE_DIR}/src/link/script.cpp"
COMPILE_FLAGS "${BISON_FLAGS}"
DEFINES_FILE "${PROJECT_SOURCE_DIR}/src/link/script.hpp"
)

set(rgbasm_src
"${BISON_PARSER_OUTPUT_SOURCE}"
"${BISON_ASM_PARSER_OUTPUT_SOURCE}"
"asm/charmap.cpp"
"asm/fixpoint.cpp"
"asm/format.cpp"
Expand All @@ -48,12 +48,12 @@ set(rgbasm_src
"asm/rpn.cpp"
"asm/section.cpp"
"asm/symbol.cpp"
"asm/util.cpp"
"asm/warning.cpp"
"extern/utf8decoder.cpp"
"hashmap.cpp"
"linkdefs.cpp"
"opmath.cpp"
"util.cpp"
)

set(rgbfix_src
Expand All @@ -74,19 +74,20 @@ set(rgbgfx_src
)

set(rgblink_src
"${BISON_LINKER_SCRIPT_PARSER_OUTPUT_SOURCE}"
"link/assign.cpp"
"link/main.cpp"
"link/object.cpp"
"link/output.cpp"
"link/patch.cpp"
"link/script.cpp"
"link/sdas_obj.cpp"
"link/section.cpp"
"link/symbol.cpp"
"extern/utf8decoder.cpp"
"hashmap.cpp"
"linkdefs.cpp"
"opmath.cpp"
"util.cpp"
)

foreach(PROG "asm" "fix" "gfx" "link")
Expand Down
39 changes: 1 addition & 38 deletions src/link/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,44 +462,7 @@ int main(int argc, char *argv[])
if (linkerScriptName) {
verbosePrint("Reading linker script...\n");

linkerScript = openFile(linkerScriptName, "r");

// Modify all sections according to the linker script
struct SectionPlacement *placement;

while ((placement = script_NextSection())) {
struct Section *section = placement->section;

assert(section->offset == 0);
// Check if this doesn't conflict with what the code says
if (section->type == SECTTYPE_INVALID) {
for (struct Section *sect = section; sect; sect = sect->nextu)
sect->type = placement->type; // SDCC "unknown" sections
} else if (section->type != placement->type) {
error(NULL, 0, "Linker script contradicts \"%s\"'s type",
section->name);
}
if (section->isBankFixed && placement->bank != section->bank)
error(NULL, 0, "Linker script contradicts \"%s\"'s bank placement",
section->name);
if (section->isAddressFixed && placement->org != section->org)
error(NULL, 0, "Linker script contradicts \"%s\"'s address placement",
section->name);
if (section->isAlignFixed
&& (placement->org & section->alignMask) != 0)
error(NULL, 0, "Linker script contradicts \"%s\"'s alignment",
section->name);

section->isAddressFixed = true;
section->org = placement->org;
section->isBankFixed = true;
section->bank = placement->bank;
section->isAlignFixed = false; // The alignment is satisfied
}

fclose(linkerScript);

script_Cleanup();
script_ProcessScript(linkerScriptName);

// If the linker script produced any errors, some sections may be in an invalid state
if (nbErrors != 0)
Expand Down
Loading

0 comments on commit a664c1d

Please sign in to comment.