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* SegFindInternal (const char* Name, unsigned HashVal)
79 /* Try to find the segment with the given name, return a pointer to the
80 * segment structure, or 0 if not found.
83 Segment* S = HashTab [HashVal];
85 if (strcmp (Name, S->Name) == 0) {
97 static Segment* NewSegment (const char* Name, unsigned HashVal, unsigned char Type)
98 /* Create a new segment and initialize it */
100 /* Get the length of the symbol name */
101 unsigned Len = strlen (Name);
103 /* Allocate memory */
104 Segment* S = xmalloc (sizeof (Segment) + Len);
106 /* Initialize the fields */
117 memcpy (S->Name, Name, Len);
118 S->Name [Len] = '\0';
120 /* Insert the segment into the segment list */
125 /* Insert the segment into the segment hash list */
126 S->Next = HashTab [HashVal];
127 HashTab [HashVal] = S;
129 /* Return the new entry */
135 Segment* GetSegment (const char* Name, unsigned char Type, const char* ObjName)
136 /* Search for a segment and return an existing one. If the segment does not
137 * exist, create a new one and return that. ObjName is only used for the error
138 * message and may be NULL if the segment is linker generated.
141 /* Create a hash over the name and try to locate the segment in the table */
142 unsigned HashVal = HashStr (Name) % HASHTAB_SIZE;
143 Segment* S = SegFindInternal (Name, HashVal);
145 /* If we don't have that segment already, allocate it using the type of
149 /* Create a new segment */
150 S = NewSegment (Name, HashVal, Type);
152 /* Check if the existing segment has the requested type */
153 if (S->Type != Type) {
154 /* Allow an empty object name */
156 ObjName = "[linker generated]";
158 Error ("Module `%s': Type mismatch for segment `%s'", ObjName, Name);
162 /* Return the segment */
168 Section* NewSection (Segment* Seg, unsigned char Align, unsigned char Type)
169 /* Create a new section for the given segment */
174 /* Allocate memory */
175 Section* S = xmalloc (sizeof (Segment));
177 /* Initialize the data */
186 /* Calculate the alignment bytes needed for the section */
187 V = (0x01UL << S->Align) - 1;
188 S->Fill = (unsigned char) (((Seg->Size + V) & ~V) - Seg->Size);
190 /* Adjust the segment size and set the section offset */
191 Seg->Size += S->Fill;
192 S->Offs = Seg->Size; /* Current size is offset */
194 /* Insert the section into the segment */
195 if (Seg->SecRoot == 0) {
196 /* First section in this segment */
199 Seg->SecLast->Next = S;
203 /* Return the struct */
209 Section* ReadSection (FILE* F, ObjData* O)
210 /* Read a section from a file */
225 /* Read the alignment */
228 /* Read the segment type */
231 /* Print some data */
233 printf ("Module `%s': Found segment `%s', size = %lu, align = %u, type = %u\n",
234 GetObjFileName (O), Name, Size, Align, Type);
237 /* Get the segment for this section */
238 S = GetSegment (Name, Type, GetObjFileName (O));
240 /* We have the segment and don't need the name any longer */
243 /* Allocate the section we will return later */
244 Sec = NewSection (S, Align, Type);
246 /* Set up the minimum segment alignment */
247 if (Sec->Align > S->Align) {
248 /* Section needs larger alignment, use this one */
249 S->Align = Sec->Align;
253 /* Start reading fragments from the file and insert them into the section . */
258 /* Read the fragment type */
259 unsigned char Type = Read8 (F);
261 /* Handle the different fragment types */
265 Frag = NewFragment (Type, ReadVar (F), Sec);
276 Frag = NewFragment (Type & FRAG_TYPEMASK, Type & FRAG_BYTEMASK, Sec);
280 /* Will allocate memory, but we don't care... */
281 Frag = NewFragment (Type, ReadVar (F), Sec);
285 Error ("Unknown fragment type in module `%s', segment `%s': %02X",
286 GetObjFileName (O), S->Name, Type);
291 /* Now read the fragment data */
292 switch (Frag->Type) {
296 ReadData (F, Frag->LitBuf, Frag->Size);
302 Frag->Expr = ReadExpr (F, O);
307 /* Read the file position of the fragment */
308 ReadFilePos (F, &Frag->Pos);
310 /* Remember the module we had this fragment from */
314 CHECK (Size >= Frag->Size);
318 /* Return the section */
324 Segment* SegFind (const char* Name)
325 /* Return the given segment or NULL if not found. */
327 return SegFindInternal (Name, HashStr (Name) % HASHTAB_SIZE);
332 int IsBSSType (Segment* S)
333 /* Check if the given segment is a BSS style segment, that is, it does not
334 * contain non-zero data.
337 /* Loop over all sections */
338 Section* Sec = S->SecRoot;
340 /* Loop over all fragments */
341 Fragment* F = Sec->FragRoot;
343 if (F->Type == FRAG_LITERAL) {
344 unsigned char* Data = F->LitBuf;
345 unsigned long Count = F->Size;
351 } else if (F->Type == FRAG_EXPR || F->Type == FRAG_SEXPR) {
352 if (GetExprVal (F->Expr) != 0) {
366 /* Dump the segments and it's contents */
372 Segment* Seg = SegRoot;
374 Section* S = Seg->SecRoot;
375 printf ("Segment: %s (%lu)\n", Seg->Name, Seg->Size);
377 Fragment* F = S->FragRoot;
378 printf (" Section:\n");
383 printf (" Literal (%lu bytes):", F->Size);
392 printf (" %02X", *Data++);
399 printf (" Expression (%lu bytes):\n", F->Size);
405 printf (" Signed expression (%lu bytes):\n", F->Size);
411 printf (" Empty space (%lu bytes)\n", F->Size);
415 Internal ("Invalid fragment type: %02X", F->Type);
427 unsigned SegWriteConstExpr (FILE* F, ExprNode* E, int Signed, unsigned Size)
428 /* Write a supposedly constant expression to the target file. Do a range
429 * check and return one of the SEG_EXPR_xxx codes.
432 static const unsigned long U_HighRange [4] = {
433 0x000000FF, 0x0000FFFF, 0x00FFFFFF, 0xFFFFFFFF
435 static const long S_HighRange [4] = {
436 0x0000007F, 0x00007FFF, 0x007FFFFF, 0x7FFFFFFF
438 static const long S_LowRange [4] = {
439 0xFFFFFF80, 0xFFFF8000, 0xFF800000, 0x80000000
443 /* Get the expression value */
444 long Val = GetExprVal (E);
447 CHECK (Size >= 1 && Size <= 4);
449 /* Check for a range error */
451 if (Val > S_HighRange [Size-1] || Val < S_LowRange [Size-1]) {
453 return SEG_EXPR_RANGE_ERROR;
456 if (((unsigned long)Val) > U_HighRange [Size-1]) {
458 return SEG_EXPR_RANGE_ERROR;
462 /* Write the value to the file */
463 WriteVal (F, Val, Size);
471 void SegWrite (FILE* Tgt, Segment* S, SegWriteFunc F, void* Data)
472 /* Write the data from the given segment to a file. For expressions, F is
473 * called (see description of SegWriteFunc above).
477 unsigned long Offs = 0;
479 /* Loop over all sections in this segment */
480 Section* Sec = S->SecRoot;
484 /* If we have fill bytes, write them now */
485 WriteMult (Tgt, S->FillVal, Sec->Fill);
487 /* Loop over all fragments in this section */
488 Frag = Sec->FragRoot;
491 switch (Frag->Type) {
494 WriteData (Tgt, Frag->LitBuf, Frag->Size);
499 Sign = (Frag->Type == FRAG_SEXPR);
500 /* Call the users function and evaluate the result */
501 switch (F (Frag->Expr, Sign, Frag->Size, Offs, Data)) {
506 case SEG_EXPR_RANGE_ERROR:
507 Error ("Range error in module `%s', line %lu",
508 GetSourceFileName (Frag->Obj, Frag->Pos.Name),
512 case SEG_EXPR_TOO_COMPLEX:
513 Error ("Expression too complex in module `%s', line %lu",
514 GetSourceFileName (Frag->Obj, Frag->Pos.Name),
519 Internal ("Invalid return code from SegWriteFunc");
524 WriteMult (Tgt, S->FillVal, Frag->Size);
528 Internal ("Invalid fragment type: %02X", Frag->Type);
531 /* Update the offset */
545 static int CmpSegStart (const void* K1, const void* K2)
546 /* Compare function for qsort */
548 /* Get the real segment pointers */
549 const Segment* S1 = *(const Segment**)K1;
550 const Segment* S2 = *(const Segment**)K2;
552 /* Compare the start addresses */
553 if (S1->PC > S2->PC) {
555 } else if (S1->PC < S2->PC) {
558 /* Sort segments with equal starts by name */
559 return strcmp (S1->Name, S2->Name);
565 void PrintSegmentMap (FILE* F)
566 /* Print a segment map to the given file */
572 /* Allocate memory for the segment pool */
573 SegPool = xmalloc (SegCount * sizeof (Segment*));
575 /* Collect pointers to the segments */
580 /* Check the count for safety */
581 CHECK (I < SegCount);
583 /* Remember the pointer */
586 /* Follow the linked list */
589 /* Next array index */
592 CHECK (I == SegCount);
594 /* Sort the array by increasing start addresses */
595 qsort (SegPool, SegCount, sizeof (Segment*), CmpSegStart);
598 fprintf (F, "Name Start End Size\n"
599 "--------------------------------------------\n");
601 /* Print the segments */
602 for (I = 0; I < SegCount; ++I) {
604 /* Get a pointer to the segment */
607 /* Print empty segments only if explicitly requested */
608 if (VerboseMap || S->Size > 0) {
609 /* Print the segment data */
610 fprintf (F, "%-20s %06lX %06lX %06lX\n",
611 S->Name, S->PC, S->PC + S->Size, S->Size);
615 /* Free the segment pool */
621 void CheckSegments (void)
622 /* Walk through the segment list and check if there are segments that were
623 * not written to the output file. Output an error if this is the case.
626 Segment* S = SegRoot;
628 if (S->Size > 0 && S->Dumped == 0) {
629 Error ("Missing memory area assignment for segment `%s'", S->Name);