#include <string.h>
/* common */
+#include "alignment.h"
#include "check.h"
+#include "coll.h"
#include "exprdefs.h"
#include "fragdefs.h"
-#include "hashstr.h"
+#include "hashfunc.h"
#include "print.h"
#include "segdefs.h"
#include "symdefs.h"
/* Hash table */
#define HASHTAB_MASK 0x3FU
#define HASHTAB_SIZE (HASHTAB_MASK + 1)
-static Segment* HashTab [HASHTAB_SIZE];
+static Segment* HashTab[HASHTAB_SIZE];
-static unsigned SegCount = 0; /* Segment count */
-static Segment* SegRoot = 0; /* List of all segments */
+/* List of all segments */
+static Collection SegmentList = STATIC_COLLECTION_INITIALIZER;
/* Initialize the fields */
S->Name = Name;
S->Next = 0;
- S->SecRoot = 0;
- S->SecLast = 0;
+ S->Sections = EmptyCollection;
S->PC = 0;
S->Size = 0;
- S->AlignObj = 0;
S->OutputName = 0;
S->OutputOffs = 0;
- S->Align = 0;
+ S->Alignment = 1;
S->FillVal = 0;
S->AddrSize = AddrSize;
S->ReadOnly = 0;
S->Dumped = 0;
/* Insert the segment into the segment list and assign the segment id */
- if (SegRoot == 0) {
- S->Id = 0;
- } else {
- S->Id = SegRoot->Id + 1;
- }
- S->List = SegRoot;
- SegRoot = S;
- ++SegCount;
+ S->Id = CollCount (&SegmentList);
+ CollAppend (&SegmentList, S);
/* Insert the segment into the segment hash list */
Hash = (S->Name & HASHTAB_MASK);
-Section* NewSection (Segment* Seg, unsigned char Align, unsigned char AddrSize)
+Section* NewSection (Segment* Seg, unsigned long Alignment, unsigned char AddrSize)
/* Create a new section for the given segment */
{
- unsigned long V;
-
-
/* Allocate memory */
Section* S = xmalloc (sizeof (Section));
/* Initialize the data */
S->Next = 0;
S->Seg = Seg;
+ S->Obj = 0;
S->FragRoot = 0;
S->FragLast = 0;
S->Size = 0;
- S->Align = Align;
+ S->Alignment= Alignment;
S->AddrSize = AddrSize;
/* Calculate the alignment bytes needed for the section */
- V = (0x01UL << S->Align) - 1;
- S->Fill = (unsigned char) (((Seg->Size + V) & ~V) - Seg->Size);
+ S->Fill = AlignCount (Seg->Size, S->Alignment);
/* 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;
+ CollAppend (&Seg->Sections, S);
/* Return the struct */
return S;
{
unsigned Name;
unsigned Size;
- unsigned char Align;
+ unsigned long Alignment;
unsigned char Type;
unsigned FragCount;
Segment* S;
/* Read the segment data */
(void) Read32 (F); /* File size of data */
Name = MakeGlobalStringId (O, ReadVar (F)); /* Segment name */
- Size = Read32 (F); /* Size of data */
- Align = Read8 (F); /* Alignment */
+ Size = ReadVar (F); /* Size of data */
+ Alignment = ReadVar (F); /* Alignment */
Type = Read8 (F); /* Segment type */
FragCount = ReadVar (F); /* Number of fragments */
/* Print some data */
- Print (stdout, 2, "Module `%s': Found segment `%s', size = %u, align = %u, type = %u\n",
- GetObjFileName (O), GetString (Name), Size, Align, Type);
+ Print (stdout, 2,
+ "Module `%s': Found segment `%s', size = %u, alignment = %lu, type = %u\n",
+ GetObjFileName (O), GetString (Name), Size, Alignment, Type);
/* Get the segment for this section */
S = GetSegment (Name, Type, GetObjFileName (O));
/* 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;
+ Sec = NewSection (S, Alignment, Type);
+
+ /* Remember the object file this section was from */
+ Sec->Obj = O;
+
+ /* Set up the combined segment alignment */
+ if (Sec->Alignment > 1) {
+ Alignment = LeastCommonMultiple (S->Alignment, Sec->Alignment);
+ if (Alignment > MAX_ALIGNMENT) {
+ Error ("Combined alignment for segment `%s' is %lu which exceeds "
+ "%lu. Last module requiring alignment was `%s'.",
+ GetString (Name), Alignment, MAX_ALIGNMENT,
+ GetObjFileName (O));
+ } else if (Alignment >= LARGE_ALIGNMENT) {
+ Warning ("Combined alignment for segment `%s' is suspiciously "
+ "large (%lu). Last module requiring alignment was `%s'.",
+ GetString (Name), Alignment, GetObjFileName (O));
+ }
+ S->Alignment = Alignment;
}
/* Start reading fragments from the file and insert them into the section . */
case FRAG_FILL:
/* Will allocate memory, but we don't care... */
- Frag = NewFragment (Type, ReadVar (F), Sec);
+ Frag = NewFragment (Type, ReadVar (F), Sec);
break;
default:
/* Read the line infos into the list of the fragment */
ReadLineInfoList (F, O, &Frag->LineInfos);
- /* Resolve the back pointers */
- FragResolveLineInfos (Frag);
-
/* Remember the module we had this fragment from */
Frag->Obj = O;
}
*/
{
/* Loop over all sections */
- Section* Sec = S->SecRoot;
- while (Sec) {
+ unsigned I;
+ for (I = 0; I < CollCount (&S->Sections); ++I) {
+
+ /* Get the next section */
+ Section* Sec = CollAtUnchecked (&S->Sections, I);
+
/* Loop over all fragments */
Fragment* F = Sec->FragRoot;
while (F) {
}
F = F->Next;
}
- Sec = Sec->Next;
}
return 1;
}
void SegDump (void)
/* Dump the segments and it's contents */
{
- unsigned I;
+ unsigned I, J;
unsigned long Count;
unsigned char* Data;
- Segment* Seg = SegRoot;
- while (Seg) {
- Section* S = Seg->SecRoot;
+ for (I = 0; I < CollCount (&SegmentList); ++I) {
+ Segment* Seg = CollAtUnchecked (&SegmentList, I);
printf ("Segment: %s (%lu)\n", GetString (Seg->Name), Seg->Size);
- while (S) {
+ for (J = 0; J < CollCount (&Seg->Sections); ++J) {
+ Section* S = CollAtUnchecked (&Seg->Sections, J);
+ unsigned J;
Fragment* F = S->FragRoot;
printf (" Section:\n");
while (F) {
switch (F->Type) {
case FRAG_LITERAL:
- printf (" Literal (%u bytes):", F->Size);
- Count = F->Size;
- Data = F->LitBuf;
- I = 100;
- while (Count--) {
- if (I > 75) {
- printf ("\n ");
- I = 3;
+ printf (" Literal (%u bytes):", F->Size);
+ Count = F->Size;
+ Data = F->LitBuf;
+ J = 100;
+ while (Count--) {
+ if (J > 75) {
+ printf ("\n ");
+ J = 3;
}
printf (" %02X", *Data++);
- I += 3;
+ J += 3;
}
printf ("\n");
break;
}
F = F->Next;
}
- S = S->Next;
}
- Seg = Seg->List;
}
}
* check and return one of the SEG_EXPR_xxx codes.
*/
{
- static const unsigned long U_HighRange [4] = {
- 0x000000FF, 0x0000FFFF, 0x00FFFFFF, 0xFFFFFFFF
+ static const unsigned long U_Hi[4] = {
+ 0x000000FFUL, 0x0000FFFFUL, 0x00FFFFFFUL, 0xFFFFFFFFUL
};
- static const long S_HighRange [4] = {
- 0x0000007F, 0x00007FFF, 0x007FFFFF, 0x7FFFFFFF
+ static const long S_Hi[4] = {
+ 0x0000007FL, 0x00007FFFL, 0x007FFFFFL, 0x7FFFFFFFL
};
- static const long S_LowRange [4] = {
- 0xFFFFFF80, 0xFFFF8000, 0xFF800000, 0x80000000
+ static const long S_Lo[4] = {
+ ~0x0000007FL, ~0x00007FFFL, ~0x007FFFFFL, ~0x7FFFFFFFL
};
/* Check for a range error */
if (Signed) {
- if (Val > S_HighRange [Size-1] || Val < S_LowRange [Size-1]) {
+ if (Val > S_Hi[Size-1] || Val < S_Lo[Size-1]) {
/* Range error */
return SEG_EXPR_RANGE_ERROR;
}
} else {
- if (((unsigned long)Val) > U_HighRange [Size-1]) {
+ if (((unsigned long)Val) > U_Hi[Size-1]) {
/* Range error */
return SEG_EXPR_RANGE_ERROR;
}
* called (see description of SegWriteFunc above).
*/
{
- Section* Sec;
+ unsigned I;
int Sign;
unsigned long Offs = 0;
S->OutputOffs = (unsigned long) ftell (Tgt);
/* Loop over all sections in this segment */
- Sec = S->SecRoot;
- while (Sec) {
+ for (I = 0; I < CollCount (&S->Sections); ++I) {
+ Section* Sec = CollAtUnchecked (&S->Sections, I);
Fragment* Frag;
+ /* Output were this section is from */
+ Print (stdout, 2, " Section from \"%s\"\n", GetObjFileName (Sec->Obj));
+
/* If we have fill bytes, write them now */
+ Print (stdout, 2, " Filling 0x%lx bytes with 0x%02x\n",
+ Sec->Fill, S->FillVal);
WriteMult (Tgt, S->FillVal, Sec->Fill);
Offs += Sec->Fill;
Frag = Sec->FragRoot;
while (Frag) {
- /* Do fragment alignment checks */
-
-
-
/* Output fragment data */
- switch (Frag->Type) {
+ switch (Frag->Type) {
- case FRAG_LITERAL:
- WriteData (Tgt, Frag->LitBuf, Frag->Size);
- break;
+ case FRAG_LITERAL:
+ WriteData (Tgt, Frag->LitBuf, Frag->Size);
+ break;
- case FRAG_EXPR:
- case FRAG_SEXPR:
+ 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)) {
break;
case SEG_EXPR_RANGE_ERROR:
- Error ("Range error in module `%s', line %lu",
+ Error ("Range error in module `%s', line %u",
GetFragmentSourceName (Frag),
GetFragmentSourceLine (Frag));
break;
case SEG_EXPR_TOO_COMPLEX:
- Error ("Expression too complex in module `%s', line %lu",
+ Error ("Expression too complex in module `%s', line %u",
GetFragmentSourceName (Frag),
GetFragmentSourceLine (Frag));
break;
- case SEG_EXPR_INVALID:
- Error ("Invalid expression in module `%s', line %lu",
+ case SEG_EXPR_INVALID:
+ Error ("Invalid expression in module `%s', line %u",
GetFragmentSourceName (Frag),
GetFragmentSourceLine (Frag));
break;
}
/* Update the offset */
+ Print (stdout, 2, " Fragment with 0x%x bytes\n",
+ Frag->Size);
Offs += Frag->Size;
/* Next fragment */
Frag = Frag->Next;
}
-
- /* Next section */
- Sec = Sec->Next;
}
}
+unsigned SegmentCount (void)
+/* Return the total number of segments */
+{
+ return CollCount (&SegmentList);
+}
+
+
+
static int CmpSegStart (const void* K1, const void* K2)
/* Compare function for qsort */
{
/* Compare the start addresses */
if (S1->PC > S2->PC) {
- return 1;
+ return 1;
} else if (S1->PC < S2->PC) {
return -1;
} else {
void PrintSegmentMap (FILE* F)
/* Print a segment map to the given file */
{
- unsigned I;
- Segment* S;
- Segment** SegPool;
/* Allocate memory for the segment pool */
- SegPool = xmalloc (SegCount * sizeof (Segment*));
-
- /* 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;
+ Segment** SegPool = xmalloc (CollCount (&SegmentList) * sizeof (Segment*));
- /* Follow the linked list */
- S = S->List;
-
- /* Next array index */
- ++I;
+ /* Copy the segment pointers */
+ unsigned I;
+ for (I = 0; I < CollCount (&SegmentList); ++I) {
+ SegPool[I] = CollAtUnchecked (&SegmentList, I);
}
- CHECK (I == SegCount);
/* Sort the array by increasing start addresses */
- qsort (SegPool, SegCount, sizeof (Segment*), CmpSegStart);
+ qsort (SegPool, CollCount (&SegmentList), sizeof (Segment*), CmpSegStart);
/* Print a header */
- fprintf (F, "Name Start End Size\n"
- "--------------------------------------------\n");
+ fprintf (F, "Name Start End Size Align\n"
+ "----------------------------------------------------\n");
/* Print the segments */
- for (I = 0; I < SegCount; ++I) {
+ for (I = 0; I < CollCount (&SegmentList); ++I) {
/* Get a pointer to the segment */
- S = SegPool [I];
+ Segment* S = SegPool[I];
/* Print empty segments only if explicitly requested */
if (VerboseMap || S->Size > 0) {
/* Point to last element addressed */
--End;
}
- fprintf (F, "%-20s %06lX %06lX %06lX\n",
- GetString (S->Name), S->PC, End, S->Size);
+ fprintf (F, "%-20s %06lX %06lX %06lX %05lX\n",
+ GetString (S->Name), S->PC, End, S->Size, S->Alignment);
}
}
/* Output the segments to the debug file */
{
/* Walk over all segments */
- Segment* S = SegRoot;
- while (S) {
+ unsigned I;
+ for (I = 0; I < CollCount (&SegmentList); ++I) {
+
+ /* Get the next segment */
+ const Segment* S = CollAtUnchecked (&SegmentList, I);
/* Print the segment data */
fprintf (F,
- "segment\tid=%u,name=\"%s\",start=0x%06lX,size=0x%04lX,addrsize=%s,type=%s",
+ "seg\tid=%u,name=\"%s\",start=0x%06lX,size=0x%04lX,addrsize=%s,type=%s",
S->Id, GetString (S->Name), S->PC, S->Size,
AddrSizeToStr (S->AddrSize),
S->ReadOnly? "ro" : "rw");
if (S->OutputName) {
- fprintf (F, ",outputname=\"%s\",outputoffs=%lu",
+ fprintf (F, ",oname=\"%s\",ooffs=%lu",
S->OutputName, S->OutputOffs);
}
fputc ('\n', F);
-
- /* Follow the linked list */
- S = S->List;
}
}
* not written to the output file. Output an error if this is the case.
*/
{
- Segment* S = SegRoot;
- while (S) {
+ unsigned I;
+ for (I = 0; I < CollCount (&SegmentList); ++I) {
+
+ /* Get the next segment */
+ const Segment* S = CollAtUnchecked (&SegmentList, I);
+
+ /* Check it */
if (S->Size > 0 && S->Dumped == 0) {
Error ("Missing memory area assignment for segment `%s'",
GetString (S->Name));
}
- S = S->List;
}
}