1 /*****************************************************************************/
5 /* Segment handling for the ld65 linker */
9 /* (C) 1998-2010, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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 /*****************************************************************************/
48 /*****************************************************************************/
50 /*****************************************************************************/
54 /* Segment structure */
55 typedef struct Segment Segment;
57 unsigned Name; /* Name index of the segment */
58 unsigned Id; /* Segment id for debug info */
59 Segment* Next; /* Hash list */
60 Segment* List; /* List of all segments */
61 struct Section* SecRoot; /* Section list */
62 struct Section* SecLast; /* Pointer to last section */
63 unsigned long PC; /* PC were this segment is located */
64 unsigned long Size; /* Size of data so far */
65 struct ObjData* AlignObj; /* Module that requested the alignment */
66 const char* OutputName; /* Name of output file or NULL */
67 unsigned long OutputOffs; /* Offset in output file */
68 unsigned char Align; /* Alignment needed */
69 unsigned char FillVal; /* Value to use for fill bytes */
70 unsigned char AddrSize; /* Address size of segment */
71 unsigned char ReadOnly; /* True for readonly segments (config) */
72 unsigned char Relocatable; /* True if the segment is relocatable */
73 unsigned char Dumped; /* Did we dump this segment? */
78 /* Section structure (a section is a part of a segment) */
79 typedef struct Section Section;
81 Section* Next; /* List of sections in a segment */
82 Segment* Seg; /* Segment that contains the section */
83 struct Fragment* FragRoot; /* Fragment list */
84 struct Fragment* FragLast; /* Pointer to last fragment */
85 unsigned long Offs; /* Offset into the segment */
86 unsigned long Size; /* Size of the section */
87 unsigned char Align; /* Alignment */
88 unsigned char Fill; /* Fill bytes for alignment */
89 unsigned char AddrSize; /* Address size of segment */
94 /* Prototype for a function that is used to write expressions to the target
95 * file (used in SegWrite). It returns one of the following values:
97 #define SEG_EXPR_OK 0U /* Ok */
98 #define SEG_EXPR_RANGE_ERROR 1U /* Range error */
99 #define SEG_EXPR_TOO_COMPLEX 2U /* Expression too complex */
100 #define SEG_EXPR_INVALID 3U /* Expression is invalid (e.g. unmapped segment) */
102 typedef unsigned (*SegWriteFunc) (ExprNode* E, /* The expression to write */
103 int Signed, /* Signed expression? */
104 unsigned Size, /* Size (=range) */
105 unsigned long Offs, /* File offset */
106 void* Data); /* Callers data */
110 /*****************************************************************************/
112 /*****************************************************************************/
116 Segment* GetSegment (unsigned Name, unsigned char AddrSize, const char* ObjName);
117 /* Search for a segment and return an existing one. If the segment does not
118 * exist, create a new one and return that. ObjName is only used for the error
119 * message and may be NULL if the segment is linker generated.
122 Section* NewSection (Segment* Seg, unsigned char Align, unsigned char AddrSize);
123 /* Create a new section for the given segment */
125 Section* ReadSection (FILE* F, struct ObjData* O);
126 /* Read a section from a file */
128 Segment* SegFind (unsigned Name);
129 /* Return the given segment or NULL if not found. */
131 int IsBSSType (Segment* S);
132 /* Check if the given segment is a BSS style segment, that is, it does not
133 * contain non-zero data.
137 /* Dump the segments and it's contents */
139 unsigned SegWriteConstExpr (FILE* F, ExprNode* E, int Signed, unsigned Size);
140 /* Write a supposedly constant expression to the target file. Do a range
141 * check and return one of the SEG_EXPR_xxx codes.
144 void SegWrite (const char* TgtName, FILE* Tgt, Segment* S, SegWriteFunc F, void* Data);
145 /* Write the data from the given segment to a file. For expressions, F is
146 * called (see description of SegWriteFunc above).
149 void PrintSegmentMap (FILE* F);
150 /* Print a segment map to the given file */
152 void PrintDbgSegments (FILE* F);
153 /* Output the segments to the debug file */
155 void CheckSegments (void);
156 /* Walk through the segment list and check if there are segments that were
157 * not written to the output file. Output an error if this is the case.
162 /* End of segments.h */