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 /*****************************************************************************/
55 /*****************************************************************************/
57 /*****************************************************************************/
61 /* Fragment structure */
62 typedef struct Fragment_ Fragment;
64 Fragment* Next; /* Next fragment in list */
65 ObjData* Obj; /* Source of fragment */
66 unsigned long Size; /* Size of data/expression */
67 ExprNode* Expr; /* Expression if FRAG_EXPR */
68 FilePos Pos; /* File position in source */
69 unsigned char Type; /* Type of fragment */
70 unsigned char LitBuf [1]; /* Dynamically alloc'ed literal buffer */
76 #define HASHTAB_SIZE 253
77 static Segment* HashTab [HASHTAB_SIZE];
79 static unsigned SegCount = 0; /* Segment count */
80 static Segment* SegRoot = 0; /* List of all segments */
84 /*****************************************************************************/
86 /*****************************************************************************/
90 static Fragment* NewFragment (unsigned char Type, unsigned long Size, Section* S)
91 /* Create a new fragment and insert it into the segment S */
94 Fragment* F = xmalloc (sizeof (Fragment) - 1 + Size); /* Portable? */
96 /* Initialize the data */
103 /* Insert the code fragment into the segment */
104 if (S->FragRoot == 0) {
108 S->FragLast->Next = F;
113 /* Return the new fragment */
119 static Segment* NewSegment (const char* Name, unsigned char Type)
120 /* Create a new segment and initialize it */
122 /* Get the length of the symbol name */
123 unsigned Len = strlen (Name);
125 /* Allocate memory */
126 Segment* S = xmalloc (sizeof (Segment) + Len);
128 /* Initialize the fields */
139 memcpy (S->Name, Name, Len);
140 S->Name [Len] = '\0';
142 /* Insert the segment into the segment list */
147 /* Return the new entry */
153 static Section* NewSection (Segment* Seg, unsigned char Align, unsigned char Type)
154 /* Create a new section for the given segment */
159 /* Allocate memory */
160 Section* S = xmalloc (sizeof (Segment));
162 /* Initialize the data */
171 /* Calculate the alignment bytes needed for the section */
172 V = (0x01UL << S->Align) - 1;
173 S->Fill = (unsigned char) (((Seg->Size + V) & ~V) - Seg->Size);
175 /* Adjust the segment size and set the section offset */
176 Seg->Size += S->Fill;
177 S->Offs = Seg->Size; /* Current size is offset */
179 /* Insert the section into the segment */
180 if (Seg->SecRoot == 0) {
181 /* First section in this segment */
184 Seg->SecLast->Next = S;
188 /* Return the struct */
194 static Segment* SegFindInternal (const char* Name, unsigned HashVal)
195 /* Try to find the segment with the given name, return a pointer to the
196 * segment structure, or 0 if not found.
199 Segment* S = HashTab [HashVal];
201 if (strcmp (Name, S->Name) == 0) {
213 Section* ReadSection (FILE* F, ObjData* O)
214 /* Read a section from a file */
230 /* Read the alignment */
233 /* Read the segment type */
236 /* Print some data */
238 printf ("Module `%s': Found segment `%s', size = %lu, align = %u, type = %u\n",
239 O->Name, Name, Size, Align, Type);
242 /* Create a hash over the name and try to locate the segment in the table */
243 HashVal = HashStr (Name) % HASHTAB_SIZE;
244 S = SegFindInternal (Name, HashVal);
246 /* If we don't have that segment already, allocate it using the type of
250 /* Create a new segment and insert it */
251 S = NewSegment (Name, Type);
252 S->Next = HashTab [HashVal];
253 HashTab [HashVal] = S;
256 /* Allocate the section we will return later */
257 Sec = NewSection (S, Align, Type);
259 /* Check if the section has the same type as the segment */
260 if (Sec->Type != S->Type) {
262 Error ("Module `%s': Type mismatch for segment `%s'", O->Name, S->Name);
265 /* Set up the minimum segment alignment */
266 if (Sec->Align > S->Align) {
267 /* Section needs larger alignment, use this one */
268 S->Align = Sec->Align;
272 /* Start reading fragments from the file and insert them into the section . */
277 /* Read the fragment type */
278 unsigned char Type = Read8 (F);
280 /* Handle the different fragment types */
284 Frag = NewFragment (FRAG_LITERAL, Read8 (F), Sec);
288 Frag = NewFragment (FRAG_LITERAL, Read16 (F), Sec);
292 Frag = NewFragment (FRAG_LITERAL, Read24 (F), Sec);
296 Frag = NewFragment (FRAG_LITERAL, Read32 (F), Sec);
307 Frag = NewFragment (Type & FRAG_TYPEMASK, Type & FRAG_BYTEMASK, Sec);
311 /* Will allocate memory, but we don't care... */
312 Frag = NewFragment (FRAG_FILL, Read16 (F), Sec);
316 Error ("Unknown fragment type in module `%s', segment `%s': %02X",
317 O->Name, S->Name, Type);
322 /* Now read the fragment data */
323 switch (Frag->Type) {
327 ReadData (F, Frag->LitBuf, Frag->Size);
333 Frag->Expr = ReadExpr (F, O);
338 /* Read the file position of the fragment */
339 ReadFilePos (F, &Frag->Pos);
341 /* Remember the module we had this fragment from */
345 CHECK (Size >= Frag->Size);
349 /* Increment the segment size by the section size */
350 S->Size += Sec->Size;
352 /* Return the section */
358 Segment* SegFind (const char* Name)
359 /* Return the given segment or NULL if not found. */
361 return SegFindInternal (Name, HashStr (Name) % HASHTAB_SIZE);
366 int IsBSSType (Segment* S)
367 /* Check if the given segment is a BSS style segment, that is, it does not
368 * contain non-zero data.
371 /* Loop over all sections */
372 Section* Sec = S->SecRoot;
374 /* Loop over all fragments */
375 Fragment* F = Sec->FragRoot;
377 if (F->Type == FRAG_LITERAL) {
378 unsigned char* Data = F->LitBuf;
379 unsigned long Count = F->Size;
385 } else if (F->Type == FRAG_EXPR || F->Type == FRAG_SEXPR) {
386 if (GetExprVal (F->Expr) != 0) {
400 /* Dump the segments and it's contents */
406 Segment* Seg = SegRoot;
408 Section* S = Seg->SecRoot;
409 printf ("Segment: %s (%lu)\n", Seg->Name, Seg->Size);
411 Fragment* F = S->FragRoot;
412 printf (" Section:\n");
417 printf (" Literal (%lu bytes):", F->Size);
426 printf (" %02X", *Data++);
433 printf (" Expression (%lu bytes):\n", F->Size);
439 printf (" Signed expression (%lu bytes):\n", F->Size);
445 printf (" Empty space (%lu bytes)\n", F->Size);
449 Internal ("Invalid fragment type: %02X", F->Type);
461 unsigned SegWriteConstExpr (FILE* F, ExprNode* E, int Signed, unsigned Size)
462 /* Write a supposedly constant expression to the target file. Do a range
463 * check and return one of the SEG_EXPR_xxx codes.
466 static const unsigned long U_HighRange [4] = {
467 0x000000FF, 0x0000FFFF, 0x00FFFFFF, 0xFFFFFFFF
469 static const long S_HighRange [4] = {
470 0x0000007F, 0x00007FFF, 0x007FFFFF, 0x7FFFFFFF
472 static const long S_LowRange [4] = {
473 0xFFFFFF80, 0xFFFF8000, 0xFF800000, 0x80000000
477 /* Get the expression value */
478 long Val = GetExprVal (E);
481 CHECK (Size >= 1 && Size <= 4);
483 /* Check for a range error */
485 if (Val > S_HighRange [Size-1] || Val < S_LowRange [Size-1]) {
487 return SEG_EXPR_RANGE_ERROR;
490 if (((unsigned long)Val) > U_HighRange [Size-1]) {
492 return SEG_EXPR_RANGE_ERROR;
496 /* Write the value to the file */
497 WriteVal (F, Val, Size);
505 void SegWrite (FILE* Tgt, Segment* S, SegWriteFunc F, void* Data)
506 /* Write the data from the given segment to a file. For expressions, F is
507 * called (see description of SegWriteFunc above).
511 unsigned long Offs = 0;
513 /* Loop over all sections in this segment */
514 Section* Sec = S->SecRoot;
518 /* If we have fill bytes, write them now */
519 WriteMult (Tgt, S->FillVal, Sec->Fill);
521 /* Loop over all fragments in this section */
522 Frag = Sec->FragRoot;
525 switch (Frag->Type) {
528 WriteData (Tgt, Frag->LitBuf, Frag->Size);
533 Sign = (Frag->Type == FRAG_SEXPR);
534 /* Call the users function and evaluate the result */
535 switch (F (Frag->Expr, Sign, Frag->Size, Offs, Data)) {
540 case SEG_EXPR_RANGE_ERROR:
541 Error ("Range error in module `%s', line %lu",
542 Frag->Obj->Files [Frag->Pos.Name], Frag->Pos.Line);
545 case SEG_EXPR_TOO_COMPLEX:
546 Error ("Expression too complex in module `%s', line %lu",
547 Frag->Obj->Files [Frag->Pos.Name], Frag->Pos.Line);
551 Internal ("Invalid return code from SegWriteFunc");
556 WriteMult (Tgt, S->FillVal, Frag->Size);
560 Internal ("Invalid fragment type: %02X", Frag->Type);
563 /* Update the offset */
577 static int CmpSegStart (const void* K1, const void* K2)
578 /* Compare function for qsort */
580 /* Get the real segment pointers */
581 const Segment* S1 = *(const Segment**)K1;
582 const Segment* S2 = *(const Segment**)K2;
584 /* Compare the start addresses */
585 if (S1->PC > S2->PC) {
587 } else if (S1->PC < S2->PC) {
590 /* Sort segments with equal starts by name */
591 return strcmp (S1->Name, S2->Name);
597 void PrintSegmentMap (FILE* F)
598 /* Print a segment map to the given file */
604 /* Allocate memory for the segment pool */
605 SegPool = xmalloc (SegCount * sizeof (Segment*));
607 /* Collect pointers to the segments */
612 /* Check the count for safety */
613 CHECK (I < SegCount);
615 /* Remember the pointer */
618 /* Follow the linked list */
621 /* Next array index */
624 CHECK (I == SegCount);
626 /* Sort the array by increasing start addresses */
627 qsort (SegPool, SegCount, sizeof (Segment*), CmpSegStart);
630 fprintf (F, "Name Start End Size\n"
631 "--------------------------------------------\n");
633 /* Print the segments */
634 for (I = 0; I < SegCount; ++I) {
636 /* Get a pointer to the segment */
639 /* Print empty segments only if explicitly requested */
640 if (VerboseMap || S->Size > 0) {
641 /* Print the segment data */
642 fprintf (F, "%-20s %06lX %06lX %06lX\n",
643 S->Name, S->PC, S->PC + S->Size, S->Size);
647 /* Free the segment pool */
653 void CheckSegments (void)
654 /* Walk through the segment list and check if there are segments that were
655 * not written to the output file. Output an error if this is the case.
658 Segment* S = SegRoot;
660 if (S->Size > 0 && S->Dumped == 0) {
661 Error ("Missing memory area assignment for segment `%s'", S->Name);