1 /*****************************************************************************/
5 /* A span of data within a segment */
9 /* (C) 2011, 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 /*****************************************************************************/
49 /*****************************************************************************/
51 /*****************************************************************************/
55 /* Definition of a span */
57 unsigned Id; /* Id of the span */
58 unsigned Sec; /* Section id of this span */
59 unsigned long Offs; /* Offset of span within segment */
60 unsigned long Size; /* Size of span */
61 unsigned Type; /* Generic type of the data */
66 /*****************************************************************************/
68 /*****************************************************************************/
72 static Span* NewSpan (unsigned Id)
73 /* Create and return a new span */
76 Span* S = xmalloc (sizeof (Span));
78 /* Initialize the fields as necessary */
81 /* Return the result */
87 Span* ReadSpan (FILE* F, ObjData* O, unsigned Id)
88 /* Read a Span from a file and return it */
92 /* Create a new Span and initialize it */
93 Span* S = NewSpan (Id);
95 S->Offs = ReadVar (F);
96 S->Size = ReadVar (F);
98 /* Read the type. An id of zero means an empty string (no need to check) */
101 S->Type = INVALID_TYPE_ID;
103 S->Type = GetTypeId (GetObjString (O, Type));
106 /* Return the new span */
112 unsigned* ReadSpanList (FILE* F)
113 /* Read a list of span ids from a file. The list is returned as an array of
114 * unsigneds, the first being the number of spans (never zero) followed by
115 * the span ids. If the number of spans is zero, NULL is returned.
120 /* First is number of Spans */
121 unsigned Count = ReadVar (F);
126 /* Allocate memory for the list and set the count */
127 Spans = xmalloc ((Count + 1) * sizeof (*Spans));
130 /* Read the spans and add them */
132 Spans[Count] = ReadVar (F);
136 /* Return the list */
142 void FreeSpan (Span* S)
143 /* Free a span structure */
145 /* Just free the structure */
151 unsigned SpanCount (void)
152 /* Return the total number of spans */
154 /* Walk over all object files */
157 for (I = 0; I < CollCount (&ObjDataList); ++I) {
159 /* Get this object file */
160 const ObjData* O = CollAtUnchecked (&ObjDataList, I);
163 Count += CollCount (&O->Spans);
171 void PrintDbgSpanList (FILE* F, const ObjData* O, const unsigned* List)
172 /* Output a string ",span=x[+y...]" for the given list. If the list is empty
173 * or NULL, output nothing. This is a helper function for other modules to
174 * print a list of spans read by ReadSpanList to the debug info file.
179 const char* Format = ",span=%u";
180 for (I = 0; I < *List; ++I) {
181 fprintf (F, Format, O->SpanBaseId + List[I+1]);
189 void PrintDbgSpans (FILE* F)
190 /* Output the spans to a debug info file */
193 StrBuf SpanType = STATIC_STRBUF_INITIALIZER;
196 /* Walk over all object files */
197 for (I = 0; I < CollCount (&ObjDataList); ++I) {
199 /* Get this object file */
200 ObjData* O = CollAtUnchecked (&ObjDataList, I);
202 /* Walk over all spans in this object file */
203 for (J = 0; J < CollCount (&O->Spans); ++J) {
206 const Span* S = CollAtUnchecked (&O->Spans, J);
208 /* Get the section for this span */
209 const Section* Sec = GetObjSection (O, S->Sec);
211 /* Output the data */
212 fprintf (F, "span\tid=%u,seg=%u,start=%lu,size=%lu",
213 O->SpanBaseId + S->Id,
218 /* If we have a type, add it */
219 if (S->Type != INVALID_TYPE_ID) {
220 fprintf (F, ",type=%u", S->Type);
223 /* Terminate the output line */
228 /* Free the string buffer */