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 /*****************************************************************************/
56 /*****************************************************************************/
58 /*****************************************************************************/
62 /* Fragment structure */
63 typedef struct Fragment_ Fragment;
65 Fragment* Next; /* Next fragment in list */
66 ObjData* Obj; /* Source of fragment */
67 unsigned long Size; /* Size of data/expression */
68 ExprNode* Expr; /* Expression if FRAG_EXPR */
69 FilePos Pos; /* File position in source */
70 unsigned char Type; /* Type of fragment */
71 unsigned char LitBuf [1]; /* Dynamically alloc'ed literal buffer */
77 #define HASHTAB_SIZE 253
78 static Segment* HashTab [HASHTAB_SIZE];
80 static unsigned SegCount = 0; /* Segment count */
81 static Segment* SegRoot = 0; /* List of all segments */
85 /*****************************************************************************/
87 /*****************************************************************************/
91 static Fragment* NewFragment (unsigned char Type, unsigned long Size, Section* S)
92 /* Create a new fragment and insert it into the segment S */
95 Fragment* F = xmalloc (sizeof (Fragment) - 1 + Size); /* Portable? */
97 /* Initialize the data */
104 /* Insert the code fragment into the segment */
105 if (S->FragRoot == 0) {
109 S->FragLast->Next = F;
114 /* Return the new fragment */
120 static Segment* NewSegment (const char* Name, unsigned char Type)
121 /* Create a new segment and initialize it */
123 /* Get the length of the symbol name */
124 unsigned Len = strlen (Name);
126 /* Allocate memory */
127 Segment* S = xmalloc (sizeof (Segment) + Len);
129 /* Initialize the fields */
140 memcpy (S->Name, Name, Len);
141 S->Name [Len] = '\0';
143 /* Insert the segment into the segment list */
148 /* Return the new entry */
154 static Section* NewSection (Segment* Seg, unsigned char Align, unsigned char Type)
155 /* Create a new section for the given segment */
160 /* Allocate memory */
161 Section* S = xmalloc (sizeof (Segment));
163 /* Initialize the data */
172 /* Calculate the alignment bytes needed for the section */
173 V = (0x01UL << S->Align) - 1;
174 S->Fill = (unsigned char) (((Seg->Size + V) & ~V) - Seg->Size);
176 /* Adjust the segment size and set the section offset */
177 Seg->Size += S->Fill;
178 S->Offs = Seg->Size; /* Current size is offset */
180 /* Insert the section into the segment */
181 if (Seg->SecRoot == 0) {
182 /* First section in this segment */
185 Seg->SecLast->Next = S;
189 /* Return the struct */
195 static Segment* SegFindInternal (const char* Name, unsigned HashVal)
196 /* Try to find the segment with the given name, return a pointer to the
197 * segment structure, or 0 if not found.
200 Segment* S = HashTab [HashVal];
202 if (strcmp (Name, S->Name) == 0) {
214 Section* ReadSection (FILE* F, ObjData* O)
215 /* Read a section from a file */
231 /* Read the alignment */
234 /* Read the segment type */
237 /* Print some data */
239 printf ("Module `%s': Found segment `%s', size = %lu, align = %u, type = %u\n",
240 O->Name, Name, Size, Align, Type);
243 /* Create a hash over the name and try to locate the segment in the table */
244 HashVal = HashStr (Name) % HASHTAB_SIZE;
245 S = SegFindInternal (Name, HashVal);
247 /* If we don't have that segment already, allocate it using the type of
251 /* Create a new segment and insert it */
252 S = NewSegment (Name, Type);
253 S->Next = HashTab [HashVal];
254 HashTab [HashVal] = S;
257 /* Allocate the section we will return later */
258 Sec = NewSection (S, Align, Type);
260 /* Check if the section has the same type as the segment */
261 if (Sec->Type != S->Type) {
263 Error ("Module `%s': Type mismatch for segment `%s'", O->Name, S->Name);
266 /* Set up the minimum segment alignment */
267 if (Sec->Align > S->Align) {
268 /* Section needs larger alignment, use this one */
269 S->Align = Sec->Align;
273 /* Start reading fragments from the file and insert them into the section . */
278 /* Read the fragment type */
279 unsigned char Type = Read8 (F);
281 /* Handle the different fragment types */
285 Frag = NewFragment (FRAG_LITERAL, Read8 (F), Sec);
289 Frag = NewFragment (FRAG_LITERAL, Read16 (F), Sec);
293 Frag = NewFragment (FRAG_LITERAL, Read24 (F), Sec);
297 Frag = NewFragment (FRAG_LITERAL, Read32 (F), Sec);
308 Frag = NewFragment (Type & FRAG_TYPEMASK, Type & FRAG_BYTEMASK, Sec);
312 /* Will allocate memory, but we don't care... */
313 Frag = NewFragment (FRAG_FILL, Read16 (F), Sec);
317 Error ("Unknown fragment type in module `%s', segment `%s': %02X",
318 O->Name, S->Name, Type);
323 /* Now read the fragment data */
324 switch (Frag->Type) {
328 ReadData (F, Frag->LitBuf, Frag->Size);
334 Frag->Expr = ReadExpr (F, O);
339 /* Read the file position of the fragment */
340 ReadFilePos (F, &Frag->Pos);
342 /* Remember the module we had this fragment from */
346 CHECK (Size >= Frag->Size);
350 /* Increment the segment size by the section size */
351 S->Size += Sec->Size;
353 /* Return the section */
359 Segment* SegFind (const char* Name)
360 /* Return the given segment or NULL if not found. */
362 return SegFindInternal (Name, HashStr (Name) % HASHTAB_SIZE);
367 int IsBSSType (Segment* S)
368 /* Check if the given segment is a BSS style segment, that is, it does not
369 * contain non-zero data.
372 /* Loop over all sections */
373 Section* Sec = S->SecRoot;
375 /* Loop over all fragments */
376 Fragment* F = Sec->FragRoot;
378 if (F->Type == FRAG_LITERAL) {
379 unsigned char* Data = F->LitBuf;
380 unsigned long Count = F->Size;
386 } else if (F->Type == FRAG_EXPR || F->Type == FRAG_SEXPR) {
387 if (GetExprVal (F->Expr) != 0) {
401 /* Dump the segments and it's contents */
407 Segment* Seg = SegRoot;
409 Section* S = Seg->SecRoot;
410 printf ("Segment: %s (%lu)\n", Seg->Name, Seg->Size);
412 Fragment* F = S->FragRoot;
413 printf (" Section:\n");
418 printf (" Literal (%lu bytes):", F->Size);
427 printf (" %02X", *Data++);
434 printf (" Expression (%lu bytes):\n", F->Size);
440 printf (" Signed expression (%lu bytes):\n", F->Size);
446 printf (" Empty space (%lu bytes)\n", F->Size);
450 Internal ("Invalid fragment type: %02X", F->Type);
462 unsigned SegWriteConstExpr (FILE* F, ExprNode* E, int Signed, unsigned Size)
463 /* Write a supposedly constant expression to the target file. Do a range
464 * check and return one of the SEG_EXPR_xxx codes.
467 static const unsigned long U_HighRange [4] = {
468 0x000000FF, 0x0000FFFF, 0x00FFFFFF, 0xFFFFFFFF
470 static const long S_HighRange [4] = {
471 0x0000007F, 0x00007FFF, 0x007FFFFF, 0x7FFFFFFF
473 static const long S_LowRange [4] = {
474 0xFFFFFF80, 0xFFFF8000, 0xFF800000, 0x80000000
478 /* Get the expression value */
479 long Val = GetExprVal (E);
482 CHECK (Size >= 1 && Size <= 4);
484 /* Check for a range error */
486 if (Val > S_HighRange [Size-1] || Val < S_LowRange [Size-1]) {
488 return SEG_EXPR_RANGE_ERROR;
491 if (((unsigned long)Val) > U_HighRange [Size-1]) {
493 return SEG_EXPR_RANGE_ERROR;
497 /* Write the value to the file */
498 WriteVal (F, Val, Size);
506 void SegWrite (FILE* Tgt, Segment* S, SegWriteFunc F, void* Data)
507 /* Write the data from the given segment to a file. For expressions, F is
508 * called (see description of SegWriteFunc above).
512 unsigned long Offs = 0;
514 /* Loop over all sections in this segment */
515 Section* Sec = S->SecRoot;
519 /* If we have fill bytes, write them now */
520 WriteMult (Tgt, S->FillVal, Sec->Fill);
522 /* Loop over all fragments in this section */
523 Frag = Sec->FragRoot;
526 switch (Frag->Type) {
529 WriteData (Tgt, Frag->LitBuf, Frag->Size);
534 Sign = (Frag->Type == FRAG_SEXPR);
535 /* Call the users function and evaluate the result */
536 switch (F (Frag->Expr, Sign, Frag->Size, Offs, Data)) {
541 case SEG_EXPR_RANGE_ERROR:
542 Error ("Range error in module `%s', line %lu",
543 Frag->Obj->Files [Frag->Pos.Name], Frag->Pos.Line);
546 case SEG_EXPR_TOO_COMPLEX:
547 Error ("Expression too complex in module `%s', line %lu",
548 Frag->Obj->Files [Frag->Pos.Name], Frag->Pos.Line);
552 Internal ("Invalid return code from SegWriteFunc");
557 WriteMult (Tgt, S->FillVal, Frag->Size);
561 Internal ("Invalid fragment type: %02X", Frag->Type);
564 /* Update the offset */
578 static int CmpSegStart (const void* K1, const void* K2)
579 /* Compare function for qsort */
581 /* Get the real segment pointers */
582 const Segment* S1 = *(const Segment**)K1;
583 const Segment* S2 = *(const Segment**)K2;
585 /* Compare the start addresses */
586 if (S1->PC > S2->PC) {
588 } else if (S1->PC < S2->PC) {
591 /* Sort segments with equal starts by name */
592 return strcmp (S1->Name, S2->Name);
598 void PrintSegmentMap (FILE* F)
599 /* Print a segment map to the given file */
605 /* Allocate memory for the segment pool */
606 SegPool = xmalloc (SegCount * sizeof (Segment*));
608 /* Collect pointers to the segments */
613 /* Check the count for safety */
614 CHECK (I < SegCount);
616 /* Remember the pointer */
619 /* Follow the linked list */
622 /* Next array index */
625 CHECK (I == SegCount);
627 /* Sort the array by increasing start addresses */
628 qsort (SegPool, SegCount, sizeof (Segment*), CmpSegStart);
631 fprintf (F, "Name Start End Size\n"
632 "--------------------------------------------\n");
634 /* Print the segments */
635 for (I = 0; I < SegCount; ++I) {
637 /* Get a pointer to the segment */
640 /* Print empty segments only if explicitly requested */
641 if (VerboseMap || S->Size > 0) {
642 /* Print the segment data */
643 fprintf (F, "%-20s %06lX %06lX %06lX\n",
644 S->Name, S->PC, S->PC + S->Size, S->Size);
648 /* Free the segment pool */
654 void CheckSegments (void)
655 /* Walk through the segment list and check if there are segments that were
656 * not written to the output file. Output an error if this is the case.
659 Segment* S = SegRoot;
661 if (S->Size > 0 && S->Dumped == 0) {
662 Error ("Missing memory area assignment for segment `%s'", S->Name);