1 /*****************************************************************************/
5 /* Segment handling for the ld65 linker */
9 /* (C) 1998-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
57 /*****************************************************************************/
59 /*****************************************************************************/
64 #define HASHTAB_SIZE 253
65 static Segment* HashTab [HASHTAB_SIZE];
67 static unsigned SegCount = 0; /* Segment count */
68 static Segment* SegRoot = 0; /* List of all segments */
72 /*****************************************************************************/
74 /*****************************************************************************/
78 static Segment* NewSegment (const char* Name, unsigned char Type)
79 /* Create a new segment and initialize it */
81 /* Get the length of the symbol name */
82 unsigned Len = strlen (Name);
85 Segment* S = xmalloc (sizeof (Segment) + Len);
87 /* Initialize the fields */
98 memcpy (S->Name, Name, Len);
101 /* Insert the segment into the segment list */
106 /* Return the new entry */
112 static Section* NewSection (Segment* Seg, unsigned char Align, unsigned char Type)
113 /* Create a new section for the given segment */
118 /* Allocate memory */
119 Section* S = xmalloc (sizeof (Segment));
121 /* Initialize the data */
130 /* Calculate the alignment bytes needed for the section */
131 V = (0x01UL << S->Align) - 1;
132 S->Fill = (unsigned char) (((Seg->Size + V) & ~V) - Seg->Size);
134 /* Adjust the segment size and set the section offset */
135 Seg->Size += S->Fill;
136 S->Offs = Seg->Size; /* Current size is offset */
138 /* Insert the section into the segment */
139 if (Seg->SecRoot == 0) {
140 /* First section in this segment */
143 Seg->SecLast->Next = S;
147 /* Return the struct */
153 static Segment* SegFindInternal (const char* Name, unsigned HashVal)
154 /* Try to find the segment with the given name, return a pointer to the
155 * segment structure, or 0 if not found.
158 Segment* S = HashTab [HashVal];
160 if (strcmp (Name, S->Name) == 0) {
172 Section* ReadSection (FILE* F, ObjData* O)
173 /* Read a section from a file */
189 /* Read the alignment */
192 /* Read the segment type */
195 /* Print some data */
197 printf ("Module `%s': Found segment `%s', size = %lu, align = %u, type = %u\n",
198 O->Name, Name, Size, Align, Type);
201 /* Create a hash over the name and try to locate the segment in the table */
202 HashVal = HashStr (Name) % HASHTAB_SIZE;
203 S = SegFindInternal (Name, HashVal);
205 /* If we don't have that segment already, allocate it using the type of
209 /* Create a new segment and insert it */
210 S = NewSegment (Name, Type);
211 S->Next = HashTab [HashVal];
212 HashTab [HashVal] = S;
215 /* We have the segment and don't need the name any longer */
218 /* Allocate the section we will return later */
219 Sec = NewSection (S, Align, Type);
221 /* Check if the section has the same type as the segment */
222 if (Sec->Type != S->Type) {
224 Error ("Module `%s': Type mismatch for segment `%s'", O->Name, S->Name);
227 /* Set up the minimum segment alignment */
228 if (Sec->Align > S->Align) {
229 /* Section needs larger alignment, use this one */
230 S->Align = Sec->Align;
234 /* Start reading fragments from the file and insert them into the section . */
239 /* Read the fragment type */
240 unsigned char Type = Read8 (F);
242 /* Handle the different fragment types */
246 Frag = NewFragment (Type, ReadVar (F), Sec);
257 Frag = NewFragment (Type & FRAG_TYPEMASK, Type & FRAG_BYTEMASK, Sec);
261 /* Will allocate memory, but we don't care... */
262 Frag = NewFragment (Type, ReadVar (F), Sec);
266 Error ("Unknown fragment type in module `%s', segment `%s': %02X",
267 O->Name, S->Name, Type);
272 /* Now read the fragment data */
273 switch (Frag->Type) {
277 ReadData (F, Frag->LitBuf, Frag->Size);
283 Frag->Expr = ReadExpr (F, O);
288 /* Read the file position of the fragment */
289 ReadFilePos (F, &Frag->Pos);
291 /* Remember the module we had this fragment from */
295 CHECK (Size >= Frag->Size);
299 /* Increment the segment size by the section size */
300 S->Size += Sec->Size;
302 /* Return the section */
308 Segment* SegFind (const char* Name)
309 /* Return the given segment or NULL if not found. */
311 return SegFindInternal (Name, HashStr (Name) % HASHTAB_SIZE);
316 int IsBSSType (Segment* S)
317 /* Check if the given segment is a BSS style segment, that is, it does not
318 * contain non-zero data.
321 /* Loop over all sections */
322 Section* Sec = S->SecRoot;
324 /* Loop over all fragments */
325 Fragment* F = Sec->FragRoot;
327 if (F->Type == FRAG_LITERAL) {
328 unsigned char* Data = F->LitBuf;
329 unsigned long Count = F->Size;
335 } else if (F->Type == FRAG_EXPR || F->Type == FRAG_SEXPR) {
336 if (GetExprVal (F->Expr) != 0) {
350 /* Dump the segments and it's contents */
356 Segment* Seg = SegRoot;
358 Section* S = Seg->SecRoot;
359 printf ("Segment: %s (%lu)\n", Seg->Name, Seg->Size);
361 Fragment* F = S->FragRoot;
362 printf (" Section:\n");
367 printf (" Literal (%lu bytes):", F->Size);
376 printf (" %02X", *Data++);
383 printf (" Expression (%lu bytes):\n", F->Size);
389 printf (" Signed expression (%lu bytes):\n", F->Size);
395 printf (" Empty space (%lu bytes)\n", F->Size);
399 Internal ("Invalid fragment type: %02X", F->Type);
411 unsigned SegWriteConstExpr (FILE* F, ExprNode* E, int Signed, unsigned Size)
412 /* Write a supposedly constant expression to the target file. Do a range
413 * check and return one of the SEG_EXPR_xxx codes.
416 static const unsigned long U_HighRange [4] = {
417 0x000000FF, 0x0000FFFF, 0x00FFFFFF, 0xFFFFFFFF
419 static const long S_HighRange [4] = {
420 0x0000007F, 0x00007FFF, 0x007FFFFF, 0x7FFFFFFF
422 static const long S_LowRange [4] = {
423 0xFFFFFF80, 0xFFFF8000, 0xFF800000, 0x80000000
427 /* Get the expression value */
428 long Val = GetExprVal (E);
431 CHECK (Size >= 1 && Size <= 4);
433 /* Check for a range error */
435 if (Val > S_HighRange [Size-1] || Val < S_LowRange [Size-1]) {
437 return SEG_EXPR_RANGE_ERROR;
440 if (((unsigned long)Val) > U_HighRange [Size-1]) {
442 return SEG_EXPR_RANGE_ERROR;
446 /* Write the value to the file */
447 WriteVal (F, Val, Size);
455 void SegWrite (FILE* Tgt, Segment* S, SegWriteFunc F, void* Data)
456 /* Write the data from the given segment to a file. For expressions, F is
457 * called (see description of SegWriteFunc above).
461 unsigned long Offs = 0;
463 /* Loop over all sections in this segment */
464 Section* Sec = S->SecRoot;
468 /* If we have fill bytes, write them now */
469 WriteMult (Tgt, S->FillVal, Sec->Fill);
471 /* Loop over all fragments in this section */
472 Frag = Sec->FragRoot;
475 switch (Frag->Type) {
478 WriteData (Tgt, Frag->LitBuf, Frag->Size);
483 Sign = (Frag->Type == FRAG_SEXPR);
484 /* Call the users function and evaluate the result */
485 switch (F (Frag->Expr, Sign, Frag->Size, Offs, Data)) {
490 case SEG_EXPR_RANGE_ERROR:
491 Error ("Range error in module `%s', line %lu",
492 Frag->Obj->Files [Frag->Pos.Name], Frag->Pos.Line);
495 case SEG_EXPR_TOO_COMPLEX:
496 Error ("Expression too complex in module `%s', line %lu",
497 Frag->Obj->Files [Frag->Pos.Name], Frag->Pos.Line);
501 Internal ("Invalid return code from SegWriteFunc");
506 WriteMult (Tgt, S->FillVal, Frag->Size);
510 Internal ("Invalid fragment type: %02X", Frag->Type);
513 /* Update the offset */
527 static int CmpSegStart (const void* K1, const void* K2)
528 /* Compare function for qsort */
530 /* Get the real segment pointers */
531 const Segment* S1 = *(const Segment**)K1;
532 const Segment* S2 = *(const Segment**)K2;
534 /* Compare the start addresses */
535 if (S1->PC > S2->PC) {
537 } else if (S1->PC < S2->PC) {
540 /* Sort segments with equal starts by name */
541 return strcmp (S1->Name, S2->Name);
547 void PrintSegmentMap (FILE* F)
548 /* Print a segment map to the given file */
554 /* Allocate memory for the segment pool */
555 SegPool = xmalloc (SegCount * sizeof (Segment*));
557 /* Collect pointers to the segments */
562 /* Check the count for safety */
563 CHECK (I < SegCount);
565 /* Remember the pointer */
568 /* Follow the linked list */
571 /* Next array index */
574 CHECK (I == SegCount);
576 /* Sort the array by increasing start addresses */
577 qsort (SegPool, SegCount, sizeof (Segment*), CmpSegStart);
580 fprintf (F, "Name Start End Size\n"
581 "--------------------------------------------\n");
583 /* Print the segments */
584 for (I = 0; I < SegCount; ++I) {
586 /* Get a pointer to the segment */
589 /* Print empty segments only if explicitly requested */
590 if (VerboseMap || S->Size > 0) {
591 /* Print the segment data */
592 fprintf (F, "%-20s %06lX %06lX %06lX\n",
593 S->Name, S->PC, S->PC + S->Size, S->Size);
597 /* Free the segment pool */
603 void CheckSegments (void)
604 /* Walk through the segment list and check if there are segments that were
605 * not written to the output file. Output an error if this is the case.
608 Segment* S = SegRoot;
610 if (S->Size > 0 && S->Dumped == 0) {
611 Error ("Missing memory area assignment for segment `%s'", S->Name);