Build everything as C++ (#1176)

This commit is contained in:
Rangi
2023-11-07 15:45:56 -05:00
committed by GitHub
parent 78d83be2b2
commit 1e70e703a7
84 changed files with 667 additions and 663 deletions

View File

@@ -6,16 +6,16 @@
#include <stdlib.h>
#include <string.h>
#include "link/assign.h"
#include "link/section.h"
#include "link/symbol.h"
#include "link/object.h"
#include "link/main.h"
#include "link/output.h"
#include "link/assign.hpp"
#include "link/section.hpp"
#include "link/symbol.hpp"
#include "link/object.hpp"
#include "link/main.hpp"
#include "link/output.hpp"
#include "error.h"
#include "helpers.h"
#include "linkdefs.h"
#include "error.hpp"
#include "helpers.hpp"
#include "linkdefs.hpp"
struct MemoryLocation {
uint16_t address;
@@ -36,14 +36,14 @@ uint64_t nbSectionsToAssign;
// Init the free space-modelling structs
static void initFreeSpace(void)
{
for (enum SectionType type = 0; type < SECTTYPE_INVALID; type++) {
memory[type] = malloc(sizeof(*memory[type]) * nbbanks(type));
for (enum SectionType type = (enum SectionType)0; type < SECTTYPE_INVALID; type = (enum SectionType)(type + 1)) {
memory[type] = (struct FreeSpace *)malloc(sizeof(*memory[type]) * nbbanks(type));
if (!memory[type])
err("Failed to init free space for region %d", type);
for (uint32_t bank = 0; bank < nbbanks(type); bank++) {
memory[type][bank].next =
malloc(sizeof(*memory[type][0].next));
(struct FreeSpace *)malloc(sizeof(*memory[type][0].next));
if (!memory[type][bank].next)
err("Failed to init free space for region %d bank %" PRIu32,
type, bank);
@@ -242,7 +242,7 @@ static void placeSection(struct Section *section)
free(freeSpace);
} else if (!noLeftSpace && !noRightSpace) {
// The free space is split in two
struct FreeSpace *newSpace = malloc(sizeof(*newSpace));
struct FreeSpace *newSpace = (struct FreeSpace *)malloc(sizeof(*newSpace));
if (!newSpace)
err("Failed to split new free space");
@@ -318,7 +318,7 @@ struct UnassignedSection {
#define BANK_CONSTRAINED (1 << 2)
#define ORG_CONSTRAINED (1 << 1)
#define ALIGN_CONSTRAINED (1 << 0)
static struct UnassignedSection *unassignedSections[1 << 3] = {0};
static struct UnassignedSection *unassignedSections[1 << 3] = {};
static struct UnassignedSection *sections;
/*
@@ -360,7 +360,7 @@ void assign_AssignSections(void)
// Initialize assignment
// Generate linked lists of sections to assign
sections = malloc(sizeof(*sections) * nbSectionsToAssign + 1);
sections = (struct UnassignedSection *)malloc(sizeof(*sections) * nbSectionsToAssign + 1);
if (!sections)
err("Failed to allocate memory for section assignment");
@@ -428,7 +428,7 @@ max_out:
void assign_Cleanup(void)
{
for (enum SectionType type = 0; type < SECTTYPE_INVALID; type++) {
for (enum SectionType type = (enum SectionType)0; type < SECTTYPE_INVALID; type = (enum SectionType)(type + 1)) {
for (uint32_t bank = 0; bank < nbbanks(type); bank++) {
struct FreeSpace *ptr =
memory[type][bank].next;

View File

@@ -12,20 +12,20 @@
#include <sys/stat.h>
#include <sys/types.h>
#include "link/assign.h"
#include "link/object.h"
#include "link/output.h"
#include "link/patch.h"
#include "link/section.h"
#include "link/script.h"
#include "link/symbol.h"
#include "link/assign.hpp"
#include "link/object.hpp"
#include "link/output.hpp"
#include "link/patch.hpp"
#include "link/section.hpp"
#include "link/script.hpp"
#include "link/symbol.hpp"
#include "extern/getopt.h"
#include "extern/getopt.hpp"
#include "error.h"
#include "linkdefs.h"
#include "platform.h"
#include "version.h"
#include "error.hpp"
#include "linkdefs.hpp"
#include "platform.hpp"
#include "version.hpp"
bool isDmgMode; // -d
char *linkerScriptName; // -l
@@ -117,7 +117,7 @@ void argErr(char flag, char const *fmt, ...)
nbErrors++;
}
_Noreturn void fatal(struct FileStackNode const *where, uint32_t lineNo, char const *fmt, ...)
[[noreturn]] void fatal(struct FileStackNode const *where, uint32_t lineNo, char const *fmt, ...)
{
va_list ap;
@@ -228,9 +228,9 @@ struct {
char const *name;
uint16_t max;
} scrambleSpecs[SCRAMBLE_UNK] = {
[SCRAMBLE_ROMX] = { "romx", 65535 },
[SCRAMBLE_SRAM] = { "sram", 255 },
[SCRAMBLE_WRAMX] = { "wramx", 7},
AT(SCRAMBLE_ROMX) { "romx", 65535 },
AT(SCRAMBLE_SRAM) { "sram", 255 },
AT(SCRAMBLE_WRAMX) { "wramx", 7 },
};
static void parseScrambleSpec(char const *spec)
@@ -249,6 +249,7 @@ static void parseScrambleSpec(char const *spec)
size_t regionNameLen = strcspn(spec, "=, \t");
// Length of region name string slice for printing, truncated if too long
int regionNamePrintLen = regionNameLen > INT_MAX ? INT_MAX : (int)regionNameLen;
enum ScrambledRegion region = (enum ScrambledRegion)0;
// If this trips, `spec` must be pointing at a ',' or '=' (or NUL) due to the assert
if (regionNameLen == 0) {
@@ -271,9 +272,7 @@ static void parseScrambleSpec(char const *spec)
}
// Now, determine which region type this is
enum ScrambledRegion region = 0;
for (; region < SCRAMBLE_UNK; region++) {
for (; region < SCRAMBLE_UNK; region = (enum ScrambledRegion)(region + 1)) {
// If the strings match (case-insensitively), we got it!
// `strncasecmp` must be used here since `regionName` points
// to the entire remaining argument.
@@ -341,7 +340,7 @@ next:
}
}
_Noreturn void reportErrors(void) {
[[noreturn]] void reportErrors(void) {
fprintf(stderr, "Linking failed with %" PRIu32 " error%s\n",
nbErrors, nbErrors == 1 ? "" : "s");
exit(1);

View File

@@ -8,18 +8,18 @@
#include <string.h>
#include <stdlib.h>
#include "link/assign.h"
#include "link/main.h"
#include "link/object.h"
#include "link/patch.h"
#include "link/sdas_obj.h"
#include "link/section.h"
#include "link/symbol.h"
#include "link/assign.hpp"
#include "link/main.hpp"
#include "link/object.hpp"
#include "link/patch.hpp"
#include "link/sdas_obj.hpp"
#include "link/section.hpp"
#include "link/symbol.hpp"
#include "error.h"
#include "helpers.h"
#include "linkdefs.h"
#include "version.h"
#include "error.hpp"
#include "helpers.hpp"
#include "linkdefs.hpp"
#include "version.hpp"
static struct SymbolList {
size_t nbSymbols;
@@ -38,7 +38,7 @@ static struct Assertion *assertions;
// Internal, DO NOT USE.
// For helper wrapper macros defined below, such as `tryReadlong`
#define tryRead(func, type, errval, var, file, ...) \
#define tryRead(func, type, errval, vartype, var, file, ...) \
do { \
FILE *tmpFile = file; \
type tmpVal = func(tmpFile); \
@@ -48,7 +48,7 @@ static struct Assertion *assertions;
? "Unexpected end of file" \
: strerror(errno)); \
} \
var = tmpVal; \
var = (vartype)tmpVal; \
} while (0)
/*
@@ -86,7 +86,7 @@ static int64_t readlong(FILE *file)
* argument is provided, the reason for failure
*/
#define tryReadlong(var, file, ...) \
tryRead(readlong, int64_t, INT64_MAX, var, file, __VA_ARGS__)
tryRead(readlong, int64_t, INT64_MAX, long, var, file, __VA_ARGS__)
// There is no `readbyte`, just use `fgetc` or `getc`.
@@ -98,8 +98,8 @@ static int64_t readlong(FILE *file)
* @param ... A format string and related arguments; note that an extra string
* argument is provided, the reason for failure
*/
#define tryGetc(var, file, ...) \
tryRead(getc, int, EOF, var, file, __VA_ARGS__)
#define tryGetc(type, var, file, ...) \
tryRead(getc, int, EOF, type, var, file, __VA_ARGS__)
/*
* Reads a '\0'-terminated string from a file.
@@ -122,7 +122,7 @@ static char *readstr(FILE *file)
// If the buffer isn't suitable to write the next char...
if (index >= capacity || !str) {
capacity *= 2;
str = realloc(str, capacity);
str = (char *)realloc(str, capacity);
// End now in case of error
if (!str)
return NULL;
@@ -149,7 +149,7 @@ static char *readstr(FILE *file)
* argument is provided, the reason for failure
*/
#define tryReadstr(var, file, ...) \
tryRead(readstr, char*, NULL, var, file, __VA_ARGS__)
tryRead(readstr, char *, NULL, char *, var, file, __VA_ARGS__)
// Functions to parse object files
@@ -170,8 +170,8 @@ static void readFileStackNode(FILE *file, struct FileStackNode fileNodes[], uint
fileNodes[i].parent = parentID == (uint32_t)-1 ? NULL : &fileNodes[parentID];
tryReadlong(fileNodes[i].lineNo, file,
"%s: Cannot read node #%" PRIu32 "'s line number: %s", fileName, i);
tryGetc(fileNodes[i].type, file, "%s: Cannot read node #%" PRIu32 "'s type: %s",
fileName, i);
tryGetc(enum FileStackNodeType, fileNodes[i].type, file,
"%s: Cannot read node #%" PRIu32 "'s type: %s", fileName, i);
switch (fileNodes[i].type) {
case NODE_FILE:
case NODE_MACRO:
@@ -182,7 +182,8 @@ static void readFileStackNode(FILE *file, struct FileStackNode fileNodes[], uint
case NODE_REPT:
tryReadlong(fileNodes[i].reptDepth, file,
"%s: Cannot read node #%" PRIu32 "'s rept depth: %s", fileName, i);
fileNodes[i].iters = malloc(sizeof(*fileNodes[i].iters) * fileNodes[i].reptDepth);
fileNodes[i].iters =
(uint32_t *)malloc(sizeof(*fileNodes[i].iters) * fileNodes[i].reptDepth);
if (!fileNodes[i].iters)
fatal(NULL, 0, "%s: Failed to alloc node #%" PRIu32 "'s iters: %s",
fileName, i, strerror(errno));
@@ -207,7 +208,7 @@ static void readSymbol(FILE *file, struct Symbol *symbol,
{
tryReadstr(symbol->name, file, "%s: Cannot read symbol name: %s",
fileName);
tryGetc(symbol->type, file, "%s: Cannot read \"%s\"'s type: %s",
tryGetc(enum ExportLevel, symbol->type, file, "%s: Cannot read \"%s\"'s type: %s",
fileName, symbol->name);
// If the symbol is defined in this file, read its definition
if (symbol->type != SYMTYPE_IMPORT) {
@@ -243,7 +244,7 @@ static void readPatch(FILE *file, struct Patch *patch, char const *fileName, cha
uint32_t i, struct FileStackNode fileNodes[])
{
uint32_t nodeID;
uint8_t type;
enum PatchType type;
tryReadlong(nodeID, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s node ID: %s",
@@ -261,7 +262,7 @@ static void readPatch(FILE *file, struct Patch *patch, char const *fileName, cha
tryReadlong(patch->pcOffset, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s PC offset: %s",
fileName, sectName, i);
tryGetc(type, file,
tryGetc(enum PatchType, type, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s type: %s",
fileName, sectName, i);
patch->type = type;
@@ -269,7 +270,7 @@ static void readPatch(FILE *file, struct Patch *patch, char const *fileName, cha
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s RPN size: %s",
fileName, sectName, i);
patch->rpnExpression = malloc(sizeof(*patch->rpnExpression) * patch->rpnSize);
patch->rpnExpression = (uint8_t *)malloc(sizeof(*patch->rpnExpression) * patch->rpnSize);
if (!patch->rpnExpression)
err("%s: Failed to alloc \"%s\"'s patch #%" PRIu32 "'s RPN expression",
fileName, sectName, i);
@@ -313,9 +314,9 @@ static void readSection(FILE *file, struct Section *section, char const *fileNam
section->name, tmp);
section->size = tmp;
section->offset = 0;
tryGetc(byte, file, "%s: Cannot read \"%s\"'s type: %s",
tryGetc(uint8_t, byte, file, "%s: Cannot read \"%s\"'s type: %s",
fileName, section->name);
section->type = byte & 0x3F;
section->type = (enum SectionType)(byte & 0x3F);
if (byte >> 7)
section->modifier = SECTION_UNION;
else if (byte >> 6)
@@ -335,7 +336,7 @@ static void readSection(FILE *file, struct Section *section, char const *fileNam
fileName, section->name);
section->isBankFixed = tmp >= 0;
section->bank = tmp;
tryGetc(byte, file, "%s: Cannot read \"%s\"'s alignment: %s",
tryGetc(uint8_t, byte, file, "%s: Cannot read \"%s\"'s alignment: %s",
fileName, section->name);
if (byte > 16)
byte = 16;
@@ -352,7 +353,7 @@ static void readSection(FILE *file, struct Section *section, char const *fileNam
if (sect_HasData(section->type)) {
// Ensure we never allocate 0 bytes
uint8_t *data = malloc(sizeof(*data) * section->size + 1);
uint8_t *data = (uint8_t *)malloc(sizeof(*data) * section->size + 1);
if (!data)
err("%s: Unable to read \"%s\"'s data", fileName,
@@ -373,7 +374,7 @@ static void readSection(FILE *file, struct Section *section, char const *fileNam
fileName, section->name);
struct Patch *patches =
malloc(sizeof(*patches) * section->nbPatches + 1);
(struct Patch *)malloc(sizeof(*patches) * section->nbPatches + 1);
if (!patches)
err("%s: Unable to read \"%s\"'s patches", fileName, section->name);
@@ -469,7 +470,8 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
// Since SDCC does not provide line info, everything will be reported as coming from the
// object file. It's better than nothing.
nodes[fileID].nbNodes = 1;
nodes[fileID].nodes = malloc(sizeof(nodes[fileID].nodes[0]) * nodes[fileID].nbNodes);
nodes[fileID].nodes =
(struct FileStackNode *)malloc(sizeof(nodes[fileID].nodes[0]) * nodes[fileID].nbNodes);
if (!nodes[fileID].nodes)
err("Failed to get memory for %s's nodes", fileName);
struct FileStackNode *where = &nodes[fileID].nodes[0];
@@ -517,7 +519,8 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
nbSectionsToAssign += nbSections;
tryReadlong(nodes[fileID].nbNodes, file, "%s: Cannot read number of nodes: %s", fileName);
nodes[fileID].nodes = calloc(nodes[fileID].nbNodes, sizeof(nodes[fileID].nodes[0]));
nodes[fileID].nodes =
(struct FileStackNode *)calloc(nodes[fileID].nbNodes, sizeof(nodes[fileID].nodes[0]));
if (!nodes[fileID].nodes)
err("Failed to get memory for %s's nodes", fileName);
verbosePrint("Reading %u nodes...\n", nodes[fileID].nbNodes);
@@ -525,12 +528,12 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
readFileStackNode(file, nodes[fileID].nodes, i, fileName);
// This file's symbols, kept to link sections to them
struct Symbol **fileSymbols = malloc(sizeof(*fileSymbols) * nbSymbols + 1);
struct Symbol **fileSymbols = (struct Symbol **)malloc(sizeof(*fileSymbols) * nbSymbols + 1);
if (!fileSymbols)
err("Failed to get memory for %s's symbols", fileName);
struct SymbolList *symbolList = malloc(sizeof(*symbolList));
struct SymbolList *symbolList = (struct SymbolList *)malloc(sizeof(*symbolList));
if (!symbolList)
err("Failed to register %s's symbol list", fileName);
@@ -539,13 +542,13 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
symbolList->next = symbolLists;
symbolLists = symbolList;
uint32_t *nbSymPerSect = calloc(nbSections ? nbSections : 1,
uint32_t *nbSymPerSect = (uint32_t *)calloc(nbSections ? nbSections : 1,
sizeof(*nbSymPerSect));
verbosePrint("Reading %" PRIu32 " symbols...\n", nbSymbols);
for (uint32_t i = 0; i < nbSymbols; i++) {
// Read symbol
struct Symbol *symbol = malloc(sizeof(*symbol));
struct Symbol *symbol = (struct Symbol *)malloc(sizeof(*symbol));
if (!symbol)
err("%s: Couldn't create new symbol", fileName);
@@ -559,13 +562,13 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
}
// This file's sections, stored in a table to link symbols to them
struct Section **fileSections = malloc(sizeof(*fileSections)
* (nbSections ? nbSections : 1));
struct Section **fileSections =
(struct Section **)malloc(sizeof(*fileSections) * (nbSections ? nbSections : 1));
verbosePrint("Reading %" PRIu32 " sections...\n", nbSections);
for (uint32_t i = 0; i < nbSections; i++) {
// Read section
fileSections[i] = malloc(sizeof(*fileSections[i]));
fileSections[i] = (struct Section *)malloc(sizeof(*fileSections[i]));
if (!fileSections[i])
err("%s: Couldn't create new section", fileName);
@@ -573,8 +576,8 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
readSection(file, fileSections[i], fileName, nodes[fileID].nodes);
fileSections[i]->fileSymbols = fileSymbols;
if (nbSymPerSect[i]) {
fileSections[i]->symbols = malloc(nbSymPerSect[i]
* sizeof(*fileSections[i]->symbols));
fileSections[i]->symbols =
(struct Symbol **)malloc(nbSymPerSect[i] * sizeof(*fileSections[i]->symbols));
if (!fileSections[i]->symbols)
err("%s: Couldn't link to symbols",
fileName);
@@ -624,7 +627,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
fileName);
verbosePrint("Reading %" PRIu32 " assertions...\n", nbAsserts);
for (uint32_t i = 0; i < nbAsserts; i++) {
struct Assertion *assertion = malloc(sizeof(*assertion));
struct Assertion *assertion = (struct Assertion *)malloc(sizeof(*assertion));
if (!assertion)
err("%s: Couldn't create new assertion", fileName);
@@ -655,7 +658,7 @@ void obj_Setup(unsigned int nbFiles)
if (nbFiles > SIZE_MAX / sizeof(*nodes))
fatal(NULL, 0, "Impossible to link more than %zu files!", SIZE_MAX / sizeof(*nodes));
nodes = malloc(sizeof(*nodes) * nbFiles);
nodes = (struct FileStackNodes *)malloc(sizeof(*nodes) * nbFiles);
}
static void freeNode(struct FileStackNode *node)

View File

@@ -7,16 +7,16 @@
#include <stdlib.h>
#include <string.h>
#include "link/output.h"
#include "link/main.h"
#include "link/section.h"
#include "link/symbol.h"
#include "link/output.hpp"
#include "link/main.hpp"
#include "link/section.hpp"
#include "link/symbol.hpp"
#include "extern/utf8decoder.h"
#include "extern/utf8decoder.hpp"
#include "error.h"
#include "linkdefs.h"
#include "platform.h" // MIN_NB_ELMS
#include "error.hpp"
#include "linkdefs.hpp"
#include "platform.hpp" // For `MIN_NB_ELMS` and `AT`
#define BANK_SIZE 0x4000
@@ -61,14 +61,14 @@ static enum SectionType typeMap[SECTTYPE_INVALID] = {
void out_AddSection(struct Section const *section)
{
static uint32_t maxNbBanks[] = {
[SECTTYPE_ROM0] = 1,
[SECTTYPE_ROMX] = UINT32_MAX,
[SECTTYPE_VRAM] = 2,
[SECTTYPE_SRAM] = UINT32_MAX,
[SECTTYPE_WRAM0] = 1,
[SECTTYPE_WRAMX] = 7,
[SECTTYPE_OAM] = 1,
[SECTTYPE_HRAM] = 1
AT(SECTTYPE_WRAM0) 1,
AT(SECTTYPE_VRAM) 2,
AT(SECTTYPE_ROMX) UINT32_MAX,
AT(SECTTYPE_ROM0) 1,
AT(SECTTYPE_HRAM) 1,
AT(SECTTYPE_WRAMX) 7,
AT(SECTTYPE_SRAM) UINT32_MAX,
AT(SECTTYPE_OAM) 1,
};
uint32_t targetBank = section->bank - sectionTypeInfo[section->type].firstBank;
@@ -81,7 +81,7 @@ void out_AddSection(struct Section const *section)
if (minNbBanks > sections[section->type].nbBanks) {
sections[section->type].banks =
realloc(sections[section->type].banks,
(struct SortedSections *)realloc(sections[section->type].banks,
sizeof(*sections[0].banks) * minNbBanks);
for (uint32_t i = sections[section->type].nbBanks; i < minNbBanks; i++) {
sections[section->type].banks[i].sections = NULL;
@@ -92,7 +92,7 @@ void out_AddSection(struct Section const *section)
if (!sections[section->type].banks)
err("Failed to realloc banks");
struct SortedSection *newSection = malloc(sizeof(*newSection));
struct SortedSection *newSection = (struct SortedSection *)malloc(sizeof(*newSection));
struct SortedSection **ptr = section->size
? &sections[section->type].banks[targetBank].sections
: &sections[section->type].banks[targetBank].zeroLenSections;
@@ -173,7 +173,7 @@ static void coverOverlayBanks(uint32_t nbOverlayBanks)
if (nbUncoveredBanks > sections[SECTTYPE_ROMX].nbBanks) {
sections[SECTTYPE_ROMX].banks =
realloc(sections[SECTTYPE_ROMX].banks,
(struct SortedSections *)realloc(sections[SECTTYPE_ROMX].banks,
sizeof(*sections[SECTTYPE_ROMX].banks) * nbUncoveredBanks);
if (!sections[SECTTYPE_ROMX].banks)
err("Failed to realloc banks for overlay");
@@ -376,7 +376,7 @@ static void writeSymBank(struct SortedSections const *bankSections,
if (!nbSymbols)
return;
struct SortedSymbol *symList = malloc(sizeof(*symList) * nbSymbols);
struct SortedSymbol *symList = (struct SortedSymbol *)malloc(sizeof(*symList) * nbSymbols);
if (!symList)
err("Failed to allocate symbol list");
@@ -604,7 +604,7 @@ static void cleanupSections(struct SortedSection *section)
static void cleanup(void)
{
for (enum SectionType type = 0; type < SECTTYPE_INVALID; type++) {
for (enum SectionType type = (enum SectionType)0; type < SECTTYPE_INVALID; type = (enum SectionType)(type + 1)) {
for (uint32_t i = 0; i < sections[type].nbBanks; i++) {
struct SortedSections *bank = &sections[type].banks[i];

View File

@@ -6,14 +6,15 @@
#include <stdlib.h>
#include <string.h>
#include "link/object.h"
#include "link/patch.h"
#include "link/section.h"
#include "link/symbol.h"
#include "link/object.hpp"
#include "link/patch.hpp"
#include "link/section.hpp"
#include "link/symbol.hpp"
#include "error.h"
#include "linkdefs.h"
#include "opmath.h"
#include "error.hpp"
#include "linkdefs.hpp"
#include "opmath.hpp"
#include "platform.hpp"
/*
* This is an "empty"-type stack. Apart from the actual values, we also remember
@@ -33,8 +34,8 @@ struct RPNStack {
static void initRPNStack(void)
{
stack.capacity = 64;
stack.values = malloc(sizeof(*stack.values) * stack.capacity);
stack.errorFlags = malloc(sizeof(*stack.errorFlags) * stack.capacity);
stack.values = (int32_t *)malloc(sizeof(*stack.values) * stack.capacity);
stack.errorFlags = (bool *)malloc(sizeof(*stack.errorFlags) * stack.capacity);
if (!stack.values || !stack.errorFlags)
err("Failed to init RPN stack");
}
@@ -54,9 +55,9 @@ static void pushRPN(int32_t value, bool comesFromError)
stack.capacity *= increase_factor;
stack.values =
realloc(stack.values, sizeof(*stack.values) * stack.capacity);
(int32_t *)realloc(stack.values, sizeof(*stack.values) * stack.capacity);
stack.errorFlags =
realloc(stack.errorFlags, sizeof(*stack.errorFlags) * stack.capacity);
(bool *)realloc(stack.errorFlags, sizeof(*stack.errorFlags) * stack.capacity);
// Static analysis tools complain that the capacity might become
// zero due to overflow, but fail to realize that it's caught by
// the overflow check above. Hence the stringent check below.
@@ -133,7 +134,7 @@ static int32_t computeRPNExpr(struct Patch const *patch,
clearRPNStack();
while (size > 0) {
enum RPNCommand command = getRPNByte(&expression, &size,
enum RPNCommand command = (enum RPNCommand)getRPNByte(&expression, &size,
patch->src, patch->lineNo);
int32_t value;
@@ -534,10 +535,10 @@ static void applyFilePatches(struct Section *section, struct Section *dataSectio
uint8_t size;
int32_t min;
int32_t max;
} const types[] = {
[PATCHTYPE_BYTE] = {1, -128, 255},
[PATCHTYPE_WORD] = {2, -32768, 65536},
[PATCHTYPE_LONG] = {4, INT32_MIN, INT32_MAX}
} const types[PATCHTYPE_INVALID] = {
AT(PATCHTYPE_BYTE) { 1, -128, 255 },
AT(PATCHTYPE_WORD) { 2, -32768, 65536 },
AT(PATCHTYPE_LONG) { 4, INT32_MIN, INT32_MAX },
};
if (!isError && (value < types[patch->type].min

View File

@@ -7,23 +7,26 @@
#include <stdlib.h>
#include <string.h>
#include "link/main.h"
#include "link/script.h"
#include "link/section.h"
#include "link/main.hpp"
#include "link/script.hpp"
#include "link/section.hpp"
#include "error.h"
#include "linkdefs.h"
#include "error.hpp"
#include "linkdefs.hpp"
#include "platform.hpp"
FILE *linkerScript;
char *includeFileName;
static uint32_t lineNo;
static struct FileNode {
struct FileNode {
FILE *file;
uint32_t lineNo;
char *name;
} *fileStack;
};
static struct FileNode *fileStack;
static uint32_t fileStackSize;
static uint32_t fileStackIndex;
@@ -38,7 +41,7 @@ static void pushFile(char *newFileName)
if (!fileStackSize) // Init file stack
fileStackSize = 4;
fileStackSize *= 2;
fileStack = realloc(fileStack, sizeof(*fileStack) * fileStackSize);
fileStack = (struct FileNode *)realloc(fileStack, sizeof(*fileStack) * fileStackSize);
if (!fileStack)
err("%s(%" PRIu32 "): Internal INCLUDE error",
linkerScriptName, lineNo);
@@ -136,13 +139,14 @@ enum LinkerScriptTokenType {
TOKEN_INVALID
};
char const *tokenTypes[] = {
[TOKEN_NEWLINE] = "newline",
[TOKEN_COMMAND] = "command",
[TOKEN_BANK] = "bank command",
[TOKEN_NUMBER] = "number",
[TOKEN_STRING] = "string",
[TOKEN_EOF] = "end of file"
char const *tokenTypes[TOKEN_INVALID] = {
AT(TOKEN_NEWLINE) "newline",
AT(TOKEN_COMMAND) "command",
AT(TOKEN_BANK) "bank command",
AT(TOKEN_INCLUDE) NULL,
AT(TOKEN_NUMBER) "number",
AT(TOKEN_STRING) "string",
AT(TOKEN_EOF) "end of file",
};
enum LinkerScriptCommand {
@@ -160,15 +164,16 @@ union LinkerScriptTokenAttr {
char *string;
};
struct LinkerScriptToken {
enum LinkerScriptTokenType type;
union LinkerScriptTokenAttr attr;
};
static char const * const commands[] = {
[COMMAND_ORG] = "ORG",
[COMMAND_ALIGN] = "ALIGN",
[COMMAND_DS] = "DS"
static char const * const commands[COMMAND_INVALID] = {
AT(COMMAND_ORG) "ORG",
AT(COMMAND_ALIGN) "ALIGN",
AT(COMMAND_DS) "DS"
};
static int nextChar(void)
@@ -249,7 +254,7 @@ static struct LinkerScriptToken *nextToken(void)
if (size >= capacity || token.attr.string == NULL) {
capacity *= 2;
token.attr.string = realloc(token.attr.string, capacity);
token.attr.string = (char *)realloc(token.attr.string, capacity);
if (!token.attr.string)
err("%s: Failed to allocate memory for string",
__func__);
@@ -265,7 +270,7 @@ static struct LinkerScriptToken *nextToken(void)
for (;;) {
if (size >= capacity || str == NULL) {
capacity *= 2;
str = realloc(str, capacity);
str = (char *)realloc(str, capacity);
if (!str)
err("%s: Failed to allocate memory for token",
__func__);
@@ -287,7 +292,7 @@ static struct LinkerScriptToken *nextToken(void)
token.type = TOKEN_INVALID;
// Try to match a command
for (enum LinkerScriptCommand i = 0; i < COMMAND_INVALID; i++) {
for (enum LinkerScriptCommand i = (enum LinkerScriptCommand)0; i < COMMAND_INVALID; i = (enum LinkerScriptCommand)(i + 1)) {
if (!strcmp(commands[i], str)) {
token.type = TOKEN_COMMAND;
token.attr.command = i;
@@ -297,7 +302,7 @@ static struct LinkerScriptToken *nextToken(void)
if (token.type == TOKEN_INVALID) {
// Try to match a bank specifier
for (enum SectionType type = 0; type < SECTTYPE_INVALID; type++) {
for (enum SectionType type = (enum SectionType)0; type < SECTTYPE_INVALID; type = (enum SectionType)(type + 1)) {
if (!strcmp(sectionTypeInfo[type].name, str)) {
token.type = TOKEN_BANK;
token.attr.secttype = type;
@@ -379,8 +384,8 @@ struct SectionPlacement *script_NextSection(void)
lineNo = 1;
// Init PC for all banks
for (enum SectionType i = 0; i < SECTTYPE_INVALID; i++) {
curaddr[i] = malloc(sizeof(*curaddr[i]) * nbbanks(i));
for (enum SectionType i = (enum SectionType)0; i < SECTTYPE_INVALID; i = (enum SectionType)(i + 1)) {
curaddr[i] = (uint16_t *)malloc(sizeof(*curaddr[i]) * nbbanks(i));
for (uint32_t b = 0; b < nbbanks(i); b++)
curaddr[i][b] = sectionTypeInfo[i].startAddr;
}
@@ -539,6 +544,6 @@ lineend:
void script_Cleanup(void)
{
for (enum SectionType type = 0; type < SECTTYPE_INVALID; type++)
for (enum SectionType type = (enum SectionType)0; type < SECTTYPE_INVALID; type = (enum SectionType)(type + 1))
free(curaddr[type]);
}

View File

@@ -9,14 +9,14 @@
#include <stdlib.h>
#include <string.h>
#include "linkdefs.h"
#include "platform.h"
#include "linkdefs.hpp"
#include "platform.hpp"
#include "link/assign.h"
#include "link/main.h"
#include "link/sdas_obj.h"
#include "link/section.h"
#include "link/symbol.h"
#include "link/assign.hpp"
#include "link/main.hpp"
#include "link/sdas_obj.hpp"
#include "link/section.hpp"
#include "link/symbol.hpp"
enum NumberType {
HEX = 16, // X
@@ -60,7 +60,7 @@ retry:
if (i >= *bufLen) {
assert(*bufLen != 0);
*bufLen *= 2;
*lineBuf = realloc(*lineBuf, *bufLen);
*lineBuf = (char *)realloc(*lineBuf, *bufLen);
if (!*lineBuf)
fatal(where, *lineNo, "Failed to realloc: %s", strerror(errno));
}
@@ -144,7 +144,7 @@ enum RelocFlags {
void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) {
size_t bufLen = 256;
char *line = malloc(bufLen);
char *line = (char *)malloc(bufLen);
char const *token;
#define getToken(ptr, ...) do { \
@@ -230,14 +230,14 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) {
uint16_t writeIndex;
};
struct FileSection *fileSections = NULL;
struct Symbol **fileSymbols = malloc(sizeof(*fileSymbols) * expectedNbSymbols);
struct Symbol **fileSymbols = (struct Symbol **)malloc(sizeof(*fileSymbols) * expectedNbSymbols);
size_t nbSections = 0, nbSymbols = 0;
if (!fileSymbols)
fatal(where, lineNo, "Failed to alloc file symbols table: %s", strerror(errno));
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 = malloc(sizeof(*data) * dataCapacity);
uint8_t *data = (uint8_t *)malloc(sizeof(*data) * dataCapacity);
if (!data)
fatal(where, lineNo, "Failed to alloc data buffer: %s", strerror(errno));
@@ -255,12 +255,13 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) {
case 'A': {
if (nbSections == expectedNbAreas)
warning(where, lineNo, "Got more 'A' lines than the expected %" PRIu32, expectedNbAreas);
fileSections = realloc(fileSections, sizeof(*fileSections) * (nbSections + 1));
fileSections = (struct FileSection *)realloc(fileSections,
sizeof(*fileSections) * (nbSections + 1));
if (!fileSections)
fatal(where, lineNo, "Failed to realloc file areas: %s", strerror(errno));
fileSections[nbSections].writeIndex = 0;
#define curSection (fileSections[nbSections].section)
curSection = malloc(sizeof(*curSection));
curSection = (struct Section *)malloc(sizeof(*curSection));
if (!curSection)
fatal(where, lineNo, "Failed to alloc new area: %s", strerror(errno));
@@ -297,7 +298,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) {
if (curSection->modifier == SECTION_NORMAL) {
size_t len = strlen(where->name) + 1 + strlen(token);
curSection->name = malloc(len + 1);
curSection->name = (char *)malloc(len + 1);
if (!curSection->name)
fatal(where, lineNo, "Failed to alloc new area's name: %s", strerror(errno));
sprintf(curSection->name, "%s %s", where->name, sectionName);
@@ -363,7 +364,8 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) {
// `realloc` is dangerous, as sections contain a pointer to `fileSymbols`.
// We can try to be nice, but if the pointer moves, it's game over!
if (nbSymbols >= expectedNbSymbols) {
struct Symbol **newFileSymbols = realloc(fileSymbols, sizeof(*fileSymbols) * (nbSymbols + 1));
struct Symbol **newFileSymbols = (struct Symbol **)realloc(fileSymbols,
sizeof(*fileSymbols) * (nbSymbols + 1));
if (!newFileSymbols)
fatal(where, lineNo, "Failed to alloc extra symbols: %s", strerror(errno));
@@ -372,7 +374,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) {
// No need to assign, obviously
}
#define symbol (fileSymbols[nbSymbols])
symbol = malloc(sizeof(*symbol));
symbol = (struct Symbol *)malloc(sizeof(*symbol));
if (!symbol)
fatal(where, lineNo, "Failed to alloc symbol: %s", strerror(errno));
@@ -434,7 +436,8 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) {
struct Section *section = fileSections[nbSections - 1].section;
++section->nbSymbols;
section->symbols = realloc(section->symbols, sizeof(section->symbols[0]) * section->nbSymbols);
section->symbols = (struct Symbol **)realloc(section->symbols,
sizeof(section->symbols[0]) * section->nbSymbols);
if (!section->symbols)
fatal(where, lineNo, "Failed to realloc \"%s\"'s symbol list: %s", section->name, strerror(errno));
section->symbols[section->nbSymbols - 1] = symbol;
@@ -455,7 +458,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) {
for (token = strtok(line, delim); token; token = strtok(NULL, delim)) {
if (dataCapacity == nbBytes) {
dataCapacity *= 2;
data = realloc(data, sizeof(*data) * dataCapacity);
data = (uint8_t *)realloc(data, sizeof(*data) * dataCapacity);
if (!data)
fatal(where, lineNo, "Failed to realloc data buffer: %s", strerror(errno));
}
@@ -468,7 +471,8 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) {
// Importantly, now we know that `nbBytes != 0`, which means "pending data"
break;
case 'R': { // Supposed to directly follow `T`
case 'R': {
// Supposed to directly follow `T`
if (nbBytes == 0) {
warning(where, lineNo, "'R' line with no 'T' line, ignoring");
break;
@@ -503,7 +507,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) {
fatal(where, lineNo, "'T' lines which don't append to their section are not supported (%" PRIu16 " != %" PRIu16 ")", addr, *writeIndex);
if (!section->data) {
assert(section->size != 0);
section->data = malloc(section->size);
section->data = (uint8_t *)malloc(section->size);
if (!section->data)
fatal(where, lineNo, "Failed to alloc data for \"%s\": %s", section->name, strerror(errno));
}
@@ -551,7 +555,8 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) {
warning(where, lineNo, "Unknown reloc flags 0x%x", flags & ~RELOC_ALL_FLAGS);
// Turn this into a Patch
section->patches = realloc(section->patches, sizeof(section->patches[0]) * (section->nbPatches + 1));
section->patches = (struct Patch *)realloc(section->patches,
sizeof(section->patches[0]) * (section->nbPatches + 1));
if (!section->patches)
fatal(where, lineNo, "Failed to alloc extra patch for \"%s\"", section->name);
struct Patch *patch = &section->patches[section->nbPatches];
@@ -578,7 +583,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) {
#define RPN_EXTRA_SIZE (5 + 1 + 5 + 1 + 5 + 1) // >> 8 & $FF, then + <baseValue>
#define allocPatch(size) do { \
patch->rpnSize = (size); \
patch->rpnExpression = malloc(patch->rpnSize + RPN_EXTRA_SIZE); \
patch->rpnExpression = (uint8_t *)malloc(patch->rpnSize + RPN_EXTRA_SIZE); \
if (!patch->rpnExpression) \
fatal(where, lineNo, "Failed to alloc RPN expression: %s", strerror(errno)); \
} while (0)
@@ -649,7 +654,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file) {
baseValue += other->size;
allocPatch(1 + strlen(name) + 1);
patch->rpnSize = 1 + strlen(name) + 1;
patch->rpnExpression = malloc(patch->rpnSize + RPN_EXTRA_SIZE);
patch->rpnExpression = (uint8_t *)malloc(patch->rpnSize + RPN_EXTRA_SIZE);
if (!patch->rpnExpression)
fatal(where, lineNo, "Failed to alloc RPN expression: %s", strerror(errno));
patch->rpnExpression[0] = RPN_STARTOF_SECT;

View File

@@ -6,12 +6,12 @@
#include <stdlib.h>
#include <string.h>
#include "link/main.h"
#include "link/section.h"
#include "link/main.hpp"
#include "link/section.hpp"
#include "error.h"
#include "hashmap.h"
#include "linkdefs.h"
#include "error.hpp"
#include "hashmap.hpp"
#include "linkdefs.hpp"
HashMap sections;
@@ -161,7 +161,7 @@ static void mergeSections(struct Section *target, struct Section *other, enum Se
if (other->data) {
if (target->data) {
// Ensure we're not allocating 0 bytes
target->data = realloc(target->data,
target->data = (uint8_t *)realloc(target->data,
sizeof(*target->data) * target->size + 1);
if (!target->data)
errx("Failed to concatenate \"%s\"'s fragments", target->name);
@@ -190,7 +190,7 @@ static void mergeSections(struct Section *target, struct Section *other, enum Se
void sect_AddSection(struct Section *section)
{
// Check if the section already exists
struct Section *other = hash_GetElement(sections, section->name);
struct Section *other = (struct Section *)hash_GetElement(sections, section->name);
if (other) {
if (section->modifier != other->modifier)

View File

@@ -4,12 +4,12 @@
#include <stdbool.h>
#include <stdlib.h>
#include "link/object.h"
#include "link/symbol.h"
#include "link/main.h"
#include "link/object.hpp"
#include "link/symbol.hpp"
#include "link/main.hpp"
#include "error.h"
#include "hashmap.h"
#include "error.hpp"
#include "hashmap.hpp"
HashMap symbols;
@@ -27,7 +27,7 @@ static void forEach(void *symbol, void *arg)
void sym_ForEach(void (*callback)(struct Symbol *, void *), void *arg)
{
struct ForEachSymbolArg callbackArg = { .callback = callback, .arg = arg};
struct ForEachSymbolArg callbackArg = { .callback = callback, .arg = arg };
hash_ForEach(symbols, forEach, &callbackArg);
}
@@ -35,7 +35,7 @@ void sym_ForEach(void (*callback)(struct Symbol *, void *), void *arg)
void sym_AddSymbol(struct Symbol *symbol)
{
// Check if the symbol already exists
struct Symbol *other = hash_GetElement(symbols, symbol->name);
struct Symbol *other = (struct Symbol *)hash_GetElement(symbols, symbol->name);
if (other) {
fprintf(stderr, "error: \"%s\" both in %s from ", symbol->name, symbol->objFileName);