summaryrefslogtreecommitdiff
path: root/misc/pascal/include/pofflib.h
blob: 0f547d9ae61d9ff46f7b09c8b1cd7ad60874492f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/***************************************************************************
 * pofflib.h
 * Interfaces to the POFF library
 *
 *   Copyright (C) 2008-2009 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ***************************************************************************/

#ifndef __POFFLIB_H
#define __POFFLIB_H

/***************************************************************************
 * Included Files
 ***************************************************************************/

#include <stdint.h>
#include "keywords.h"
#include "poff.h"

/***************************************************************************
 * Definitions
 ***************************************************************************/

/***************************************************************************
 * Public Types
 ***************************************************************************/

/* The internal form of the POFF data structures are hidden from the caller
 * in these "handles"
 */

typedef void *poffHandle_t;
typedef void *poffProgHandle_t;
typedef void *poffSymHandle_t;

/* This is a externally visible form of a symbol table entry that is
 * not entangled in the POFF internal string table logic.
 */

struct poffLibSymbol_s
{
  /* type is the type of symbol described by this entry.
   * See the STT_ definitions in poff.h.
   */

  uint8_t type;

  /* For data section symbols, the following provides the required
   * data space alignment for the symbol memory representation.  For
   * procedures and functions, this value is ignored. See the STT_
   * definitions in poff.h
   */

  uint8_t align;

  /* These flags describe the characteristics of the symbol.  See the
   * STF_ definitions above.
   */

  uint8_t flags;

  /* name is a reference to the symbol name in the string table
   * section data.
   */

  const char *name;

  /* value is the value associated with symbol.  For defined data
   * section symbols, this is the offset into the initialized data
   * section data; for defined procedures and functions, this the
   * offset into program section data.  For undefined symbols, this
   * valid can be used as as addend.
   */

  uint32_t value;

  /* For data section symbols, this is the size of the initialized
   * data region associated with the symbol.
   */

  uint32_t size;
};
typedef struct poffLibSymbol_s poffLibSymbol_t;

/* The externally visible form of a line number structure.  Line numbers
 * are associated with executable program data sections.
 */

struct poffLibLineNumber_s
{
  /* This is the source file line number */

  uint32_t lineno;

  /* This is the full filename of the file containing the line number. */

  const char *filename;

  /* This is an offset to the beginning code in the program data section
   * associated with this line number.
   */

   uint32_t offset;
};
typedef struct poffLibLineNumber_s poffLibLineNumber_t;

/* The externally visible form of a debug function info structure.
 */

struct poffLibDebugFuncInfo_s
{
  /* For use outside of libpoff so that the allocated debug
   * information can be retained in a list.
   */

  struct poffLibDebugFuncInfo_s *next;

  /* This is the address or label of the function/procedure entry
   * point.
   */

  uint32_t value;

  /* This is the size of the value returned by the function in
   * bytes (zero for procedures).
   */

  uint32_t retsize;

  /* This is the number of parameters accepted by the function/
   * procedure.
   */

  uint32_t nparms;

  /* This is the beginning of a table of input parameter sizes
   * the actually allocate size will be nparms entries.
   */

  uint32_t argsize[1];
};
typedef struct poffLibDebugFuncInfo_s poffLibDebugFuncInfo_t;

#define SIZEOFDEBUFINFO(n) (sizeof(poffLibDebugFuncInfo_t) + ((n)-1)*sizeof(uint32_t))

/***************************************************************************
 * Public Variables
 ***************************************************************************/

/***************************************************************************
 * Public Function Prototypes
 ***************************************************************************/

/* Functions to create/destroy a handle to POFF file data */

extern poffHandle_t poffCreateHandle(void);
extern void         poffDestroyHandle(poffHandle_t handle);
extern void         poffResetAccess(poffHandle_t handle);

/* Functions to manage writing a POFF file */

extern void         poffSetFileType(poffHandle_t handle, uint8_t fh_type,
                    uint16_t nfiles, const char *name);
extern void         poffSetArchitecture(poffHandle_t handle, uint8_t fh_arch);
extern void         poffSetEntryPoint(poffHandle_t handle, uint32_t entryPoint);
extern int32_t      poffFindString(poffHandle_t handle, const char *string);
extern uint32_t     poffAddString(poffHandle_t handle, const char *string);
extern uint32_t     poffAddFileName(poffHandle_t handle, const char *name);
extern void         poffAddProgByte(poffHandle_t handle, uint8_t progByte);
#if 0 /* not used */
extern uint32_t     poffAddRoDataByte(poffHandle_t handle, uint8_t dataByte);
#endif
extern uint32_t     poffAddRoDataString(poffHandle_t handle,
                    const char *string);
extern uint32_t     poffAddSymbol(poffHandle_t handle,
                  poffLibSymbol_t *symbol);
extern uint32_t     poffAddLineNumber(poffHandle_t handle,
                      uint16_t lineNumber, uint16_t fileNumber,
                      uint32_t progSectionDataOffset);
extern uint32_t     poffAddDebugFuncInfo(poffHandle_t handle,
                     poffLibDebugFuncInfo_t *pContainer);
extern uint32_t     poffAddRelocation(poffHandle_t handle,
                      uint8_t relocType, uint32_t symIndex,
                      uint32_t sectionDataOffset);
extern void         poffWriteFile(poffHandle_t handle, FILE *poffFile);

/* Functions to manage reading a POFF file */

extern uint16_t     poffReadFile(poffHandle_t handle, FILE *poffFile);
extern uint8_t      poffGetFileType(poffHandle_t handle);
extern uint8_t      poffGetArchitecture(poffHandle_t handle);
extern uint32_t     poffGetEntryPoint(poffHandle_t handle);
extern const char  *poffGetFileHdrName(poffHandle_t handle);
extern uint32_t     poffGetRoDataSize(poffHandle_t handle);
extern int32_t      poffGetFileName(poffHandle_t handle, const char **fname);
extern int          poffGetProgByte(poffHandle_t handle);
extern int32_t      poffGetSymbol(poffHandle_t handle,
                  poffLibSymbol_t *symbol);
extern const char  *poffGetString(poffHandle_t handle, uint32_t index);
extern int32_t      poffGetLineNumber(poffHandle_t handle,
                      poffLibLineNumber_t *lineno);
extern int32_t      poffGetRawLineNumber(poffHandle_t handle,
                     poffLineNumber_t *lineno);
extern int32_t      poffGetRawRelocation(poffHandle_t handle,
                     poffRelocation_t *reloc);
extern poffLibDebugFuncInfo_t *poffGetDebugFuncInfo(poffHandle_t handle);
extern poffLibDebugFuncInfo_t *poffCreateDebugInfoContainer(uint32_t nparms);
extern void         poffReleaseDebugFuncContainer(poffLibDebugFuncInfo_t *pDebugFuncInfo);
extern void         poffDiscardDebugFuncInfo(poffHandle_t handle);
extern int32_t      poffProgTell(poffHandle_t handle);
extern int          poffProgSeek(poffHandle_t handle, uint32_t offset);
extern uint32_t     poffGetProgSize(poffHandle_t handle);
extern void         poffReleaseProgData(poffHandle_t handle);

/* Functions used to manage modifications to a POFF file using a
 * temporary container for the new program data.
 */

extern poffProgHandle_t poffCreateProgHandle(void);
extern void         poffDestroyProgHandle(poffProgHandle_t handle);
extern void         poffResetProgHandle(poffProgHandle_t handle);
extern uint16_t     poffAddTmpProgByte(poffProgHandle_t handle,
                       uint8_t progByte);
extern uint16_t     poffWriteTmpProgBytes(uint8_t *buffer, uint32_t nbyte,
                      poffProgHandle_t handle);
extern void         poffReplaceProgData(poffHandle_t handle,
                    poffProgHandle_t progHandle);

/* Functions used to manage modifications to a POFF file using a
 * temporary container for the new symbol data.
 */

extern poffSymHandle_t poffCreateSymHandle(void);
extern void         poffDestroySymHandle(poffSymHandle_t handle);
extern void         poffResetSymHandle(poffSymHandle_t handle);
extern uint32_t     poffAddTmpSymbol(poffHandle_t handle, poffSymHandle_t symHandle,
                     poffLibSymbol_t *symbol);
extern void         poffReplaceSymbolTable(poffHandle_t handle,
                       poffSymHandle_t symHandle);

/* Functions used to extract/insert whole data sections from/into a POFF
 * file container
 */

extern uint32_t     poffExtractProgramData(poffHandle_t handle,
                       uint8_t **progData);
extern void         poffInsertProgramData(poffHandle_t handle,
                      uint8_t *progData, uint32_t progSize);
extern uint32_t     poffExtractRoData(poffHandle_t handle,
                      uint8_t **roData);
extern void         poffAppendRoData(poffHandle_t handle,
                     uint8_t *roData, uint32_t roDataSize);

/* Functions to manage printing of the POFF file content */

extern void         poffDumpFileHeader(poffHandle_t handle, FILE *outFile);
extern void         poffDumpSectionHeaders(poffHandle_t handle, FILE *outFile);
extern void         poffDumpSymbolTable(poffHandle_t handle, FILE *outFile);
extern void         poffDumpRelocTable(poffHandle_t handle, FILE *outFile);

/* Helper functions to manage resolution of labels in POFF files.  These
 * just store and retrieve information by label number.
 */

extern void         poffAddToDefinedLabelTable(uint32_t label, uint32_t pc);
extern void         poffAddToUndefinedLabelTable(uint32_t label,
                                                 uint32_t symIndex);
extern int          poffGetSymIndexForUndefinedLabel(uint32_t label);
extern int          poffGetPcForDefinedLabel(uint32_t label);
extern void         poffReleaseLabelReferences(void);

/* Helper functions for line numbers */

extern void         poffReadLineNumberTable(poffHandle_t handle);
extern poffLibLineNumber_t *poffFindLineNumber(uint32_t offset);
extern void         poffReleaseLineNumberTable(void);

/* Helper functions for debug information */

extern void         poffReadDebugFuncInfoTable(poffHandle_t handle);
extern poffLibDebugFuncInfo_t *poffFindDebugFuncInfo(uint32_t offset);
extern void         poffReplaceDebugFuncInfo(poffHandle_t handle);
extern void         poffReleaseDebugFuncInfoTable(void);

#endif /* __POFFLIB_H */