1 | /*****************************************************************************/ |
2 | /* */ |
3 | /* segments.h */ |
4 | /* */ |
5 | /* Segment handling for the ld65 linker */ |
6 | /* */ |
7 | /* */ |
8 | /* */ |
9 | /* (C) 1998-2012, Ullrich von Bassewitz */ |
10 | /* Roemerstrasse 52 */ |
11 | /* D-70794 Filderstadt */ |
12 | /* EMail: uz@cc65.org */ |
13 | /* */ |
14 | /* */ |
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. */ |
18 | /* */ |
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: */ |
22 | /* */ |
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 */ |
30 | /* distribution. */ |
31 | /* */ |
32 | /*****************************************************************************/ |
33 | |
34 | |
35 | |
36 | #ifndef SEGMENTS_H |
37 | #define SEGMENTS_H |
38 | |
39 | |
40 | |
41 | #include <stdio.h> |
42 | |
43 | /* common */ |
44 | #include "coll.h" |
45 | #include "exprdefs.h" |
46 | |
47 | |
48 | |
49 | /*****************************************************************************/ |
50 | /* Data */ |
51 | /*****************************************************************************/ |
52 | |
53 | |
54 | |
55 | /* Forwards */ |
56 | struct MemoryArea; |
57 | |
58 | /* Segment structure */ |
59 | typedef struct Segment Segment; |
60 | struct Segment { |
61 | unsigned Name; /* Name index of the segment */ |
62 | unsigned Id; /* Segment id for debug info */ |
63 | Segment* Next; /* Hash list */ |
64 | unsigned Flags; /* Segment flags */ |
65 | Collection Sections; /* Sections in this segment */ |
66 | struct MemoryArea* MemArea; /* Run memory area once placed */ |
67 | unsigned long PC; /* PC were this segment is located */ |
68 | unsigned long Size; /* Size of data so far */ |
69 | const char* OutputName; /* Name of output file or NULL */ |
70 | unsigned long OutputOffs; /* Offset in output file */ |
71 | unsigned long Alignment; /* Alignment needed */ |
72 | unsigned char FillVal; /* Value to use for fill bytes */ |
73 | unsigned char AddrSize; /* Address size of segment */ |
74 | unsigned char ReadOnly; /* True for readonly segments (config) */ |
75 | unsigned char Dumped; /* Did we dump this segment? */ |
76 | }; |
77 | |
78 | |
79 | |
80 | /* Section structure (a section is a part of a segment) */ |
81 | typedef struct Section Section; |
82 | struct Section { |
83 | Section* Next; /* List of sections in a segment */ |
84 | Segment* Seg; /* Segment that contains the section */ |
85 | struct ObjData* Obj; /* Object file this section comes from */ |
86 | struct Fragment* FragRoot; /* Fragment list */ |
87 | struct Fragment* FragLast; /* Pointer to last fragment */ |
88 | unsigned long Offs; /* Offset into the segment */ |
89 | unsigned long Size; /* Size of the section */ |
90 | unsigned long Fill; /* Fill bytes for alignment */ |
91 | unsigned long Alignment; /* Alignment */ |
92 | unsigned char AddrSize; /* Address size of segment */ |
93 | }; |
94 | |
95 | |
96 | |
97 | /* Prototype for a function that is used to write expressions to the target |
98 | ** file (used in SegWrite). It returns one of the following values: |
99 | */ |
100 | #define SEG_EXPR_OK 0U /* Ok */ |
101 | #define SEG_EXPR_RANGE_ERROR 1U /* Range error */ |
102 | #define SEG_EXPR_TOO_COMPLEX 2U /* Expression too complex */ |
103 | #define SEG_EXPR_INVALID 3U /* Expression is invalid (e.g. unmapped segment) */ |
104 | |
105 | typedef unsigned (*SegWriteFunc) (ExprNode* E, /* The expression to write */ |
106 | int Signed, /* Signed expression? */ |
107 | unsigned Size, /* Size (=range) */ |
108 | unsigned long Offs, /* File offset */ |
109 | void* Data); /* Callers data */ |
110 | |
111 | |
112 | |
113 | /*****************************************************************************/ |
114 | /* Code */ |
115 | /*****************************************************************************/ |
116 | |
117 | |
118 | |
119 | Segment* GetSegment (unsigned Name, unsigned char AddrSize, const char* ObjName); |
120 | /* Search for a segment and return an existing one. If the segment does not |
121 | ** exist, create a new one and return that. ObjName is only used for the error |
122 | ** message and may be NULL if the segment is linker generated. |
123 | */ |
124 | |
125 | Section* NewSection (Segment* Seg, unsigned long Alignment, unsigned char AddrSize); |
126 | /* Create a new section for the given segment */ |
127 | |
128 | Section* ReadSection (FILE* F, struct ObjData* O); |
129 | /* Read a section from a file */ |
130 | |
131 | Segment* SegFind (unsigned Name); |
132 | /* Return the given segment or NULL if not found. */ |
133 | |
134 | int IsBSSType (Segment* S); |
135 | /* Check if the given segment is a BSS style segment, that is, it does not |
136 | ** contain non-zero data. |
137 | */ |
138 | |
139 | void SegDump (void); |
140 | /* Dump the segments and it's contents */ |
141 | |
142 | unsigned SegWriteConstExpr (FILE* F, ExprNode* E, int Signed, unsigned Size); |
143 | /* Write a supposedly constant expression to the target file. Do a range |
144 | ** check and return one of the SEG_EXPR_xxx codes. |
145 | */ |
146 | |
147 | void SegWrite (const char* TgtName, FILE* Tgt, Segment* S, SegWriteFunc F, void* Data); |
148 | /* Write the data from the given segment to a file. For expressions, F is |
149 | ** called (see description of SegWriteFunc above). |
150 | */ |
151 | |
152 | unsigned SegmentCount (void); |
153 | /* Return the total number of segments */ |
154 | |
155 | void PrintSegmentMap (FILE* F); |
156 | /* Print a segment map to the given file */ |
157 | |
158 | void PrintDbgSegments (FILE* F); |
159 | /* Output the segments to the debug file */ |
160 | |
161 | void CheckSegments (void); |
162 | /* Walk through the segment list and check if there are segments that were |
163 | ** not written to the output file. Output an error if this is the case. |
164 | */ |
165 | |
166 | |
167 | |
168 | /* End of segments.h */ |
169 | |
170 | #endif |
171 | |