From e548ecc6fadcf7adde3920c88b3a1efacaf3ff8f Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Wed, 14 Aug 2024 01:35:23 +0200 Subject: [PATCH] Report attempts to assign a SDCC area in a way inconsistent with its data This actually avoids breaking the (segfaulting) invariant that a section whose type "has data" has as many bytes of `->data` as its `->size`. --- src/link/script.y | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/link/script.y b/src/link/script.y index 3f3c6aec..53faf0eb 100644 --- a/src/link/script.y +++ b/src/link/script.y @@ -594,9 +594,28 @@ static void placeSection(std::string const &name, bool isOptional) { assume(section->offset == 0); // Check that the linker script doesn't contradict what the code says. if (section->type == SECTTYPE_INVALID) { - // SDCC areas don't have a type assigned yet, so the linker script is used to give them one. - for (Section *fragment = section; fragment; fragment = fragment->nextu.get()) { - fragment->type = activeType; + // A section that has data must get assigned a type that requires data. + if (!sect_HasData(activeType) && !section->data.empty()) { + scriptError( + context, + "\"%s\" is specified to be a %s section, but it contains data", + name.c_str(), + typeInfo.name.c_str() + ); + } else if (sect_HasData(activeType) && section->data.empty() && section->size != 0) { + // A section that lacks data can only be assigned to a type that requires data + // if it's empty. + scriptError( + context, + "\"%s\" is specified to be a %s section, but it doesn't contain data", + name.c_str(), + typeInfo.name.c_str() + ); + } else { + // SDCC areas don't have a type assigned yet, so the linker script is used to give them one. + for (Section *fragment = section; fragment; fragment = fragment->nextu.get()) { + fragment->type = activeType; + } } } else if (section->type != activeType) { scriptError(