2000-05-28 13:40:48 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* */
|
|
|
|
|
/* segments.c */
|
|
|
|
|
/* */
|
|
|
|
|
/* Segment handling for the ld65 linker */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
2003-05-25 17:57:50 +00:00
|
|
|
|
/* (C) 1998-2003 Ullrich von Bassewitz */
|
|
|
|
|
/* R<>merstrasse 52 */
|
|
|
|
|
/* D-70794 Filderstadt */
|
2001-05-23 19:03:40 +00:00
|
|
|
|
/* EMail: uz@cc65.org */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
|
/* */
|
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
|
/* */
|
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
|
/* distribution. */
|
|
|
|
|
/* */
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2000-07-30 21:27:37 +00:00
|
|
|
|
/* common */
|
2000-08-01 15:17:43 +00:00
|
|
|
|
#include "check.h"
|
2000-07-30 21:27:37 +00:00
|
|
|
|
#include "exprdefs.h"
|
2003-06-04 12:40:14 +00:00
|
|
|
|
#include "fragdefs.h"
|
2000-07-30 21:27:37 +00:00
|
|
|
|
#include "hashstr.h"
|
2001-03-10 10:21:03 +00:00
|
|
|
|
#include "print.h"
|
2000-07-30 21:27:37 +00:00
|
|
|
|
#include "segdefs.h"
|
|
|
|
|
#include "symdefs.h"
|
|
|
|
|
#include "xmalloc.h"
|
2000-08-01 15:17:43 +00:00
|
|
|
|
|
2000-07-30 21:27:37 +00:00
|
|
|
|
/* ld65 */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "error.h"
|
|
|
|
|
#include "expr.h"
|
2000-06-14 09:57:42 +00:00
|
|
|
|
#include "fileio.h"
|
2000-11-02 22:11:25 +00:00
|
|
|
|
#include "fragment.h"
|
2000-06-14 09:57:42 +00:00
|
|
|
|
#include "global.h"
|
2001-05-23 19:03:40 +00:00
|
|
|
|
#include "lineinfo.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "segments.h"
|
2003-06-04 12:40:14 +00:00
|
|
|
|
#include "spool.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* Data */
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Hash table */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
#define HASHTAB_MASK 0x3FU
|
|
|
|
|
#define HASHTAB_SIZE (HASHTAB_MASK + 1)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
static Segment* HashTab [HASHTAB_SIZE];
|
|
|
|
|
|
|
|
|
|
static unsigned SegCount = 0; /* Segment count */
|
|
|
|
|
static Segment* SegRoot = 0; /* List of all segments */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* Code */
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-04 12:40:14 +00:00
|
|
|
|
static Segment* NewSegment (unsigned Name, unsigned char Type)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Create a new segment and initialize it */
|
|
|
|
|
{
|
2003-06-04 12:40:14 +00:00
|
|
|
|
unsigned Hash;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Allocate memory */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Segment* S = xmalloc (sizeof (Segment));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Initialize the fields */
|
2003-06-06 12:45:19 +00:00
|
|
|
|
S->Name = Name;
|
|
|
|
|
S->Next = 0;
|
|
|
|
|
S->SecRoot = 0;
|
|
|
|
|
S->SecLast = 0;
|
|
|
|
|
S->PC = 0;
|
|
|
|
|
S->Size = 0;
|
|
|
|
|
S->AlignObj = 0;
|
|
|
|
|
S->Align = 0;
|
|
|
|
|
S->FillVal = 0;
|
|
|
|
|
S->Type = Type;
|
2003-09-14 21:08:05 +00:00
|
|
|
|
S->ReadOnly = 0;
|
2003-06-06 12:45:19 +00:00
|
|
|
|
S->Relocatable = 0;
|
|
|
|
|
S->Dumped = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Insert the segment into the segment list */
|
|
|
|
|
S->List = SegRoot;
|
|
|
|
|
SegRoot = S;
|
|
|
|
|
++SegCount;
|
|
|
|
|
|
2000-11-20 21:56:48 +00:00
|
|
|
|
/* Insert the segment into the segment hash list */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Hash = (S->Name & HASHTAB_MASK);
|
|
|
|
|
S->Next = HashTab[Hash];
|
|
|
|
|
HashTab[Hash] = S;
|
2000-11-20 21:56:48 +00:00
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Return the new entry */
|
|
|
|
|
return S;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Segment* GetSegment (unsigned Name, unsigned char Type, const char* ObjName)
|
2000-11-20 21:56:48 +00:00
|
|
|
|
/* Search for a segment and return an existing one. If the segment does not
|
|
|
|
|
* exist, create a new one and return that. ObjName is only used for the error
|
|
|
|
|
* message and may be NULL if the segment is linker generated.
|
|
|
|
|
*/
|
|
|
|
|
{
|
2003-06-04 12:40:14 +00:00
|
|
|
|
/* Try to locate the segment in the table */
|
|
|
|
|
Segment* S = SegFind (Name);
|
2000-11-20 21:56:48 +00:00
|
|
|
|
|
|
|
|
|
/* If we don't have that segment already, allocate it using the type of
|
|
|
|
|
* the first section.
|
|
|
|
|
*/
|
|
|
|
|
if (S == 0) {
|
|
|
|
|
/* Create a new segment */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
S = NewSegment (Name, Type);
|
2000-11-20 21:56:48 +00:00
|
|
|
|
} else {
|
|
|
|
|
/* Check if the existing segment has the requested type */
|
|
|
|
|
if (S->Type != Type) {
|
|
|
|
|
/* Allow an empty object name */
|
|
|
|
|
if (ObjName == 0) {
|
2000-11-23 19:14:15 +00:00
|
|
|
|
ObjName = "[linker generated]";
|
2000-11-20 21:56:48 +00:00
|
|
|
|
}
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Error ("Module `%s': Type mismatch for segment `%s'", ObjName,
|
|
|
|
|
GetString (Name));
|
2000-11-20 21:56:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Return the segment */
|
|
|
|
|
return S;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Section* NewSection (Segment* Seg, unsigned char Align, unsigned char Type)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Create a new section for the given segment */
|
|
|
|
|
{
|
|
|
|
|
unsigned long V;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Allocate memory */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Section* S = xmalloc (sizeof (Section));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Initialize the data */
|
|
|
|
|
S->Next = 0;
|
|
|
|
|
S->Seg = Seg;
|
|
|
|
|
S->FragRoot = 0;
|
|
|
|
|
S->FragLast = 0;
|
2000-11-20 21:56:48 +00:00
|
|
|
|
S->Size = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
S->Align = Align;
|
|
|
|
|
S->Type = Type;
|
|
|
|
|
|
|
|
|
|
/* Calculate the alignment bytes needed for the section */
|
|
|
|
|
V = (0x01UL << S->Align) - 1;
|
2000-06-08 20:55:04 +00:00
|
|
|
|
S->Fill = (unsigned char) (((Seg->Size + V) & ~V) - Seg->Size);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Adjust the segment size and set the section offset */
|
|
|
|
|
Seg->Size += S->Fill;
|
|
|
|
|
S->Offs = Seg->Size; /* Current size is offset */
|
|
|
|
|
|
|
|
|
|
/* Insert the section into the segment */
|
|
|
|
|
if (Seg->SecRoot == 0) {
|
|
|
|
|
/* First section in this segment */
|
|
|
|
|
Seg->SecRoot = S;
|
|
|
|
|
} else {
|
|
|
|
|
Seg->SecLast->Next = S;
|
|
|
|
|
}
|
|
|
|
|
Seg->SecLast = S;
|
|
|
|
|
|
|
|
|
|
/* Return the struct */
|
|
|
|
|
return S;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Section* ReadSection (FILE* F, ObjData* O)
|
|
|
|
|
/* Read a section from a file */
|
|
|
|
|
{
|
2003-06-04 12:40:14 +00:00
|
|
|
|
unsigned Name;
|
2003-06-03 22:19:46 +00:00
|
|
|
|
unsigned Size;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
unsigned char Align;
|
|
|
|
|
unsigned char Type;
|
2003-06-03 22:19:46 +00:00
|
|
|
|
unsigned FragCount;
|
|
|
|
|
Segment* S;
|
|
|
|
|
Section* Sec;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2003-06-03 22:19:46 +00:00
|
|
|
|
/* Read the segment data */
|
|
|
|
|
(void) Read32 (F); /* File size of data */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Name = MakeGlobalStringId (O, ReadVar (F)); /* Segment name */
|
2003-06-03 22:19:46 +00:00
|
|
|
|
Size = Read32 (F); /* Size of data */
|
|
|
|
|
Align = Read8 (F); /* Alignment */
|
|
|
|
|
Type = Read8 (F); /* Segment type */
|
|
|
|
|
FragCount = ReadVar (F); /* Number of fragments */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Print some data */
|
2003-06-03 22:19:46 +00:00
|
|
|
|
Print (stdout, 2, "Module `%s': Found segment `%s', size = %u, align = %u, type = %u\n",
|
2003-06-04 12:40:14 +00:00
|
|
|
|
GetObjFileName (O), GetString (Name), Size, Align, Type);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-11-20 21:56:48 +00:00
|
|
|
|
/* Get the segment for this section */
|
|
|
|
|
S = GetSegment (Name, Type, GetObjFileName (O));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Allocate the section we will return later */
|
|
|
|
|
Sec = NewSection (S, Align, Type);
|
|
|
|
|
|
|
|
|
|
/* Set up the minimum segment alignment */
|
|
|
|
|
if (Sec->Align > S->Align) {
|
|
|
|
|
/* Section needs larger alignment, use this one */
|
|
|
|
|
S->Align = Sec->Align;
|
|
|
|
|
S->AlignObj = O;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Start reading fragments from the file and insert them into the section . */
|
2003-06-03 22:19:46 +00:00
|
|
|
|
while (FragCount--) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
Fragment* Frag;
|
2001-05-23 19:03:40 +00:00
|
|
|
|
unsigned LineInfoIndex;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Read the fragment type */
|
|
|
|
|
unsigned char Type = Read8 (F);
|
|
|
|
|
|
2003-05-25 17:57:50 +00:00
|
|
|
|
/* Extract the check mask from the type */
|
2003-06-03 22:19:46 +00:00
|
|
|
|
unsigned char Bytes = Type & FRAG_BYTEMASK;
|
|
|
|
|
Type &= FRAG_TYPEMASK;
|
2003-05-25 17:57:50 +00:00
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Handle the different fragment types */
|
|
|
|
|
switch (Type) {
|
|
|
|
|
|
2000-08-02 13:23:06 +00:00
|
|
|
|
case FRAG_LITERAL:
|
|
|
|
|
Frag = NewFragment (Type, ReadVar (F), Sec);
|
2003-06-03 22:19:46 +00:00
|
|
|
|
ReadData (F, Frag->LitBuf, Frag->Size);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2003-06-03 22:19:46 +00:00
|
|
|
|
case FRAG_EXPR:
|
|
|
|
|
case FRAG_SEXPR:
|
|
|
|
|
Frag = NewFragment (Type, Bytes, Sec);
|
|
|
|
|
Frag->Expr = ReadExpr (F, O);
|
2000-07-30 21:27:37 +00:00
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
case FRAG_FILL:
|
2000-07-30 21:27:37 +00:00
|
|
|
|
/* Will allocate memory, but we don't care... */
|
2000-08-02 13:23:06 +00:00
|
|
|
|
Frag = NewFragment (Type, ReadVar (F), Sec);
|
2000-07-30 21:27:37 +00:00
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
default:
|
2000-07-30 21:27:37 +00:00
|
|
|
|
Error ("Unknown fragment type in module `%s', segment `%s': %02X",
|
2003-06-04 12:40:14 +00:00
|
|
|
|
GetObjFileName (O), GetString (S->Name), Type);
|
2000-07-30 21:27:37 +00:00
|
|
|
|
/* NOTREACHED */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Read the file position of the fragment */
|
|
|
|
|
ReadFilePos (F, &Frag->Pos);
|
|
|
|
|
|
2001-05-23 19:03:40 +00:00
|
|
|
|
/* Read the additional line info and resolve it */
|
|
|
|
|
LineInfoIndex = ReadVar (F);
|
|
|
|
|
if (LineInfoIndex) {
|
|
|
|
|
--LineInfoIndex;
|
2001-05-29 07:39:46 +00:00
|
|
|
|
if (LineInfoIndex >= O->LineInfoCount) {
|
|
|
|
|
Internal ("In module `%s', file `%s', line %lu: Invalid line "
|
|
|
|
|
"info with index %u (max count %u)",
|
|
|
|
|
GetObjFileName (O),
|
|
|
|
|
GetSourceFileName (O, Frag->Pos.Name),
|
|
|
|
|
Frag->Pos.Line, LineInfoIndex, O->LineInfoCount);
|
|
|
|
|
}
|
2001-05-23 19:03:40 +00:00
|
|
|
|
/* Point from the fragment to the line info... */
|
|
|
|
|
Frag->LI = O->LineInfos[LineInfoIndex];
|
|
|
|
|
/* ...and back from the line info to the fragment */
|
|
|
|
|
CollAppend (&Frag->LI->Fragments, Frag);
|
|
|
|
|
}
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Remember the module we had this fragment from */
|
|
|
|
|
Frag->Obj = O;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Return the section */
|
|
|
|
|
return Sec;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Segment* SegFind (unsigned Name)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Return the given segment or NULL if not found. */
|
|
|
|
|
{
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Segment* S = HashTab[Name & HASHTAB_MASK];
|
|
|
|
|
while (S) {
|
|
|
|
|
if (Name == S->Name) {
|
|
|
|
|
/* Found */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
S = S->Next;
|
|
|
|
|
}
|
|
|
|
|
/* Not found */
|
|
|
|
|
return S;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int IsBSSType (Segment* S)
|
|
|
|
|
/* Check if the given segment is a BSS style segment, that is, it does not
|
|
|
|
|
* contain non-zero data.
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
/* Loop over all sections */
|
|
|
|
|
Section* Sec = S->SecRoot;
|
|
|
|
|
while (Sec) {
|
|
|
|
|
/* Loop over all fragments */
|
|
|
|
|
Fragment* F = Sec->FragRoot;
|
|
|
|
|
while (F) {
|
|
|
|
|
if (F->Type == FRAG_LITERAL) {
|
|
|
|
|
unsigned char* Data = F->LitBuf;
|
|
|
|
|
unsigned long Count = F->Size;
|
|
|
|
|
while (Count--) {
|
|
|
|
|
if (*Data++ != 0) {
|
2003-09-14 21:08:05 +00:00
|
|
|
|
return 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (F->Type == FRAG_EXPR || F->Type == FRAG_SEXPR) {
|
|
|
|
|
if (GetExprVal (F->Expr) != 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
F = F->Next;
|
|
|
|
|
}
|
|
|
|
|
Sec = Sec->Next;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SegDump (void)
|
|
|
|
|
/* Dump the segments and it's contents */
|
|
|
|
|
{
|
|
|
|
|
unsigned I;
|
|
|
|
|
unsigned long Count;
|
|
|
|
|
unsigned char* Data;
|
|
|
|
|
|
|
|
|
|
Segment* Seg = SegRoot;
|
|
|
|
|
while (Seg) {
|
|
|
|
|
Section* S = Seg->SecRoot;
|
2003-06-04 12:40:14 +00:00
|
|
|
|
printf ("Segment: %s (%lu)\n", GetString (Seg->Name), Seg->Size);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
while (S) {
|
|
|
|
|
Fragment* F = S->FragRoot;
|
|
|
|
|
printf (" Section:\n");
|
|
|
|
|
while (F) {
|
|
|
|
|
switch (F->Type) {
|
|
|
|
|
|
|
|
|
|
case FRAG_LITERAL:
|
2003-06-03 22:19:46 +00:00
|
|
|
|
printf (" Literal (%u bytes):", F->Size);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
Count = F->Size;
|
|
|
|
|
Data = F->LitBuf;
|
|
|
|
|
I = 100;
|
|
|
|
|
while (Count--) {
|
|
|
|
|
if (I > 75) {
|
|
|
|
|
printf ("\n ");
|
2000-11-02 22:11:25 +00:00
|
|
|
|
I = 3;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
printf (" %02X", *Data++);
|
|
|
|
|
I += 3;
|
|
|
|
|
}
|
|
|
|
|
printf ("\n");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FRAG_EXPR:
|
2003-06-03 22:19:46 +00:00
|
|
|
|
printf (" Expression (%u bytes):\n", F->Size);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
printf (" ");
|
|
|
|
|
DumpExpr (F->Expr);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FRAG_SEXPR:
|
2003-06-03 22:19:46 +00:00
|
|
|
|
printf (" Signed expression (%u bytes):\n", F->Size);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
printf (" ");
|
|
|
|
|
DumpExpr (F->Expr);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FRAG_FILL:
|
2003-06-03 22:19:46 +00:00
|
|
|
|
printf (" Empty space (%u bytes)\n", F->Size);
|
2000-11-02 22:11:25 +00:00
|
|
|
|
break;
|
2003-06-04 12:40:14 +00:00
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
default:
|
|
|
|
|
Internal ("Invalid fragment type: %02X", F->Type);
|
|
|
|
|
}
|
|
|
|
|
F = F->Next;
|
|
|
|
|
}
|
|
|
|
|
S = S->Next;
|
|
|
|
|
}
|
|
|
|
|
Seg = Seg->List;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned SegWriteConstExpr (FILE* F, ExprNode* E, int Signed, unsigned Size)
|
|
|
|
|
/* Write a supposedly constant expression to the target file. Do a range
|
|
|
|
|
* check and return one of the SEG_EXPR_xxx codes.
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
static const unsigned long U_HighRange [4] = {
|
|
|
|
|
0x000000FF, 0x0000FFFF, 0x00FFFFFF, 0xFFFFFFFF
|
|
|
|
|
};
|
|
|
|
|
static const long S_HighRange [4] = {
|
|
|
|
|
0x0000007F, 0x00007FFF, 0x007FFFFF, 0x7FFFFFFF
|
|
|
|
|
};
|
|
|
|
|
static const long S_LowRange [4] = {
|
|
|
|
|
0xFFFFFF80, 0xFFFF8000, 0xFF800000, 0x80000000
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Get the expression value */
|
|
|
|
|
long Val = GetExprVal (E);
|
|
|
|
|
|
|
|
|
|
/* Check the size */
|
|
|
|
|
CHECK (Size >= 1 && Size <= 4);
|
|
|
|
|
|
|
|
|
|
/* Check for a range error */
|
|
|
|
|
if (Signed) {
|
|
|
|
|
if (Val > S_HighRange [Size-1] || Val < S_LowRange [Size-1]) {
|
|
|
|
|
/* Range error */
|
|
|
|
|
return SEG_EXPR_RANGE_ERROR;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (((unsigned long)Val) > U_HighRange [Size-1]) {
|
|
|
|
|
/* Range error */
|
|
|
|
|
return SEG_EXPR_RANGE_ERROR;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Write the value to the file */
|
|
|
|
|
WriteVal (F, Val, Size);
|
|
|
|
|
|
|
|
|
|
/* Success */
|
|
|
|
|
return SEG_EXPR_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SegWrite (FILE* Tgt, Segment* S, SegWriteFunc F, void* Data)
|
|
|
|
|
/* Write the data from the given segment to a file. For expressions, F is
|
|
|
|
|
* called (see description of SegWriteFunc above).
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
int Sign;
|
|
|
|
|
unsigned long Offs = 0;
|
|
|
|
|
|
|
|
|
|
/* Loop over all sections in this segment */
|
|
|
|
|
Section* Sec = S->SecRoot;
|
|
|
|
|
while (Sec) {
|
|
|
|
|
Fragment* Frag;
|
|
|
|
|
|
|
|
|
|
/* If we have fill bytes, write them now */
|
|
|
|
|
WriteMult (Tgt, S->FillVal, Sec->Fill);
|
2001-05-29 07:39:46 +00:00
|
|
|
|
Offs += Sec->Fill;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Loop over all fragments in this section */
|
|
|
|
|
Frag = Sec->FragRoot;
|
|
|
|
|
while (Frag) {
|
2003-06-03 22:19:46 +00:00
|
|
|
|
|
2003-05-25 17:57:50 +00:00
|
|
|
|
/* Do fragment alignment checks */
|
|
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2003-05-25 17:57:50 +00:00
|
|
|
|
/* Output fragment data */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
switch (Frag->Type) {
|
|
|
|
|
|
|
|
|
|
case FRAG_LITERAL:
|
|
|
|
|
WriteData (Tgt, Frag->LitBuf, Frag->Size);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FRAG_EXPR:
|
|
|
|
|
case FRAG_SEXPR:
|
|
|
|
|
Sign = (Frag->Type == FRAG_SEXPR);
|
|
|
|
|
/* Call the users function and evaluate the result */
|
|
|
|
|
switch (F (Frag->Expr, Sign, Frag->Size, Offs, Data)) {
|
|
|
|
|
|
|
|
|
|
case SEG_EXPR_OK:
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEG_EXPR_RANGE_ERROR:
|
|
|
|
|
Error ("Range error in module `%s', line %lu",
|
2000-11-23 19:14:15 +00:00
|
|
|
|
GetSourceFileName (Frag->Obj, Frag->Pos.Name),
|
2003-05-25 17:57:50 +00:00
|
|
|
|
Frag->Pos.Line);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEG_EXPR_TOO_COMPLEX:
|
|
|
|
|
Error ("Expression too complex in module `%s', line %lu",
|
2001-12-01 13:34:51 +00:00
|
|
|
|
GetSourceFileName (Frag->Obj, Frag->Pos.Name),
|
|
|
|
|
Frag->Pos.Line);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEG_EXPR_INVALID:
|
|
|
|
|
Error ("Invalid expression in module `%s', line %lu",
|
|
|
|
|
GetSourceFileName (Frag->Obj, Frag->Pos.Name),
|
|
|
|
|
Frag->Pos.Line);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
Internal ("Invalid return code from SegWriteFunc");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FRAG_FILL:
|
|
|
|
|
WriteMult (Tgt, S->FillVal, Frag->Size);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
Internal ("Invalid fragment type: %02X", Frag->Type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Update the offset */
|
|
|
|
|
Offs += Frag->Size;
|
|
|
|
|
|
|
|
|
|
/* Next fragment */
|
|
|
|
|
Frag = Frag->Next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Next section */
|
|
|
|
|
Sec = Sec->Next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int CmpSegStart (const void* K1, const void* K2)
|
|
|
|
|
/* Compare function for qsort */
|
|
|
|
|
{
|
|
|
|
|
/* Get the real segment pointers */
|
|
|
|
|
const Segment* S1 = *(const Segment**)K1;
|
|
|
|
|
const Segment* S2 = *(const Segment**)K2;
|
|
|
|
|
|
|
|
|
|
/* Compare the start addresses */
|
|
|
|
|
if (S1->PC > S2->PC) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else if (S1->PC < S2->PC) {
|
|
|
|
|
return -1;
|
|
|
|
|
} else {
|
|
|
|
|
/* Sort segments with equal starts by name */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
return strcmp (GetString (S1->Name), GetString (S2->Name));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PrintSegmentMap (FILE* F)
|
|
|
|
|
/* Print a segment map to the given file */
|
|
|
|
|
{
|
|
|
|
|
unsigned I;
|
|
|
|
|
Segment* S;
|
|
|
|
|
Segment** SegPool;
|
|
|
|
|
|
|
|
|
|
/* Allocate memory for the segment pool */
|
2000-06-14 09:57:42 +00:00
|
|
|
|
SegPool = xmalloc (SegCount * sizeof (Segment*));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Collect pointers to the segments */
|
|
|
|
|
I = 0;
|
|
|
|
|
S = SegRoot;
|
|
|
|
|
while (S) {
|
|
|
|
|
|
|
|
|
|
/* Check the count for safety */
|
|
|
|
|
CHECK (I < SegCount);
|
|
|
|
|
|
|
|
|
|
/* Remember the pointer */
|
|
|
|
|
SegPool [I] = S;
|
|
|
|
|
|
|
|
|
|
/* Follow the linked list */
|
|
|
|
|
S = S->List;
|
|
|
|
|
|
|
|
|
|
/* Next array index */
|
|
|
|
|
++I;
|
|
|
|
|
}
|
|
|
|
|
CHECK (I == SegCount);
|
|
|
|
|
|
|
|
|
|
/* Sort the array by increasing start addresses */
|
|
|
|
|
qsort (SegPool, SegCount, sizeof (Segment*), CmpSegStart);
|
|
|
|
|
|
|
|
|
|
/* Print a header */
|
|
|
|
|
fprintf (F, "Name Start End Size\n"
|
|
|
|
|
"--------------------------------------------\n");
|
|
|
|
|
|
|
|
|
|
/* Print the segments */
|
|
|
|
|
for (I = 0; I < SegCount; ++I) {
|
|
|
|
|
|
|
|
|
|
/* Get a pointer to the segment */
|
|
|
|
|
S = SegPool [I];
|
|
|
|
|
|
2003-09-14 21:08:05 +00:00
|
|
|
|
/* Print empty segments only if explicitly requested */
|
|
|
|
|
if (VerboseMap || S->Size > 0) {
|
|
|
|
|
/* Print the segment data */
|
|
|
|
|
long End = S->PC + S->Size;
|
|
|
|
|
if (S->Size > 0) {
|
|
|
|
|
/* Point to last element addressed */
|
|
|
|
|
--End;
|
|
|
|
|
}
|
|
|
|
|
fprintf (F, "%-20s %06lX %06lX %06lX\n",
|
2003-06-04 12:40:14 +00:00
|
|
|
|
GetString (S->Name), S->PC, End, S->Size);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Free the segment pool */
|
2000-06-14 09:57:42 +00:00
|
|
|
|
xfree (SegPool);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-09-14 21:08:05 +00:00
|
|
|
|
void PrintDbgSegments (FILE* F)
|
|
|
|
|
/* Output the segments to the debug file */
|
|
|
|
|
{
|
|
|
|
|
Segment* S;
|
|
|
|
|
|
|
|
|
|
/* Walk over all segments */
|
|
|
|
|
S = SegRoot;
|
|
|
|
|
while (S) {
|
|
|
|
|
|
|
|
|
|
/* Ignore empty segments */
|
|
|
|
|
if (S->Size > 0) {
|
|
|
|
|
|
|
|
|
|
/* Print the segment data */
|
|
|
|
|
fprintf (F, "segment\t\"%s\", 0x%06lX, 0x%04lX, %s, %s\n",
|
|
|
|
|
GetString (S->Name), S->PC, S->Size,
|
|
|
|
|
SegTypeToStr (S->Type),
|
|
|
|
|
S->ReadOnly? "ro" : "rw");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Follow the linked list */
|
|
|
|
|
S = S->List;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
void CheckSegments (void)
|
|
|
|
|
/* Walk through the segment list and check if there are segments that were
|
|
|
|
|
* not written to the output file. Output an error if this is the case.
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
Segment* S = SegRoot;
|
|
|
|
|
while (S) {
|
2003-09-14 21:08:05 +00:00
|
|
|
|
if (S->Size > 0 && S->Dumped == 0) {
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Error ("Missing memory area assignment for segment `%s'",
|
|
|
|
|
GetString (S->Name));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
S = S->List;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|