1 | /* |
---|
2 | * COPYRIGHT (c) 2012,2019 Chris Johns <chrisj@rtems.org> |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.rtems.org/license/LICENSE. |
---|
7 | */ |
---|
8 | /** |
---|
9 | * @file |
---|
10 | * |
---|
11 | * @ingroup rtems_rtl |
---|
12 | * |
---|
13 | * @brief RTEMS Run-Time Linker Object Support. |
---|
14 | */ |
---|
15 | |
---|
16 | #if !defined (_RTEMS_RTL_OBJ_H_) |
---|
17 | #define _RTEMS_RTL_OBJ_H_ |
---|
18 | |
---|
19 | #include <rtems.h> |
---|
20 | #include <rtems/chain.h> |
---|
21 | #include <rtems/rtl/rtl-sym.h> |
---|
22 | #include <rtems/rtl/rtl-unresolved.h> |
---|
23 | |
---|
24 | #ifdef __cplusplus |
---|
25 | extern "C" { |
---|
26 | #endif /* __cplusplus */ |
---|
27 | |
---|
28 | /** |
---|
29 | * Loader format flags. |
---|
30 | */ |
---|
31 | #define RTEMS_RTL_FMT_ELF (1 << 0) |
---|
32 | #define RTEMS_RTL_FMT_COMP (1 << 1) |
---|
33 | #define RTEMS_RTL_FMT_PRIVATE (1 << 16) |
---|
34 | |
---|
35 | /** |
---|
36 | * Loader format definition. |
---|
37 | */ |
---|
38 | typedef struct rtems_rtl_loader_format |
---|
39 | { |
---|
40 | /** |
---|
41 | * The format label. This can be used to determine and manage |
---|
42 | * specific formats. |
---|
43 | */ |
---|
44 | const char* label; |
---|
45 | |
---|
46 | /** |
---|
47 | * The format flags. |
---|
48 | */ |
---|
49 | uint32_t flags; |
---|
50 | } rtems_rtl_loader_format; |
---|
51 | |
---|
52 | /** |
---|
53 | * The type of the format loader check handler. This handler checks the format |
---|
54 | * and if it is detected as suitable it returns true. |
---|
55 | */ |
---|
56 | typedef bool (*rtems_rtl_loader_check) (rtems_rtl_obj* obj, int fd); |
---|
57 | |
---|
58 | /** |
---|
59 | * The type of the format loader load handler. This handler loads the specific |
---|
60 | * format. |
---|
61 | */ |
---|
62 | typedef bool (*rtems_rtl_loader_load) (rtems_rtl_obj* obj, int fd); |
---|
63 | |
---|
64 | /** |
---|
65 | * The type of the format loader unload handler. This handler unloads the |
---|
66 | * specific format. |
---|
67 | */ |
---|
68 | typedef bool (*rtems_rtl_loader_unload) (rtems_rtl_obj* obj); |
---|
69 | |
---|
70 | /** |
---|
71 | * The type of the format loader signature handler. This handler checks the |
---|
72 | * format signature. |
---|
73 | */ |
---|
74 | typedef rtems_rtl_loader_format* (*rtems_rtl_loader_sig) (void); |
---|
75 | |
---|
76 | /** |
---|
77 | * Table for supported loadable formats. |
---|
78 | */ |
---|
79 | typedef struct rtems_rtl_loader_table |
---|
80 | { |
---|
81 | rtems_rtl_loader_check check; /**< The check handler. */ |
---|
82 | rtems_rtl_loader_load load; /**< The loader. */ |
---|
83 | rtems_rtl_loader_unload unload; /**< The unloader. */ |
---|
84 | rtems_rtl_loader_sig signature; /**< The loader's signature. */ |
---|
85 | } rtems_rtl_loader_table; |
---|
86 | |
---|
87 | /** |
---|
88 | * Flags for the various section types. |
---|
89 | */ |
---|
90 | #define RTEMS_RTL_OBJ_SECT_TEXT (1 << 0) /**< Section holds program text. */ |
---|
91 | #define RTEMS_RTL_OBJ_SECT_CONST (1 << 1) /**< Section holds program text. */ |
---|
92 | #define RTEMS_RTL_OBJ_SECT_DATA (1 << 2) /**< Section holds program data. */ |
---|
93 | #define RTEMS_RTL_OBJ_SECT_BSS (1 << 3) /**< Section holds program bss. */ |
---|
94 | #define RTEMS_RTL_OBJ_SECT_EH (1 << 4) /**< Section holds exception data. */ |
---|
95 | #define RTEMS_RTL_OBJ_SECT_TLS (1 << 5) /**< Section holds TLS data. */ |
---|
96 | #define RTEMS_RTL_OBJ_SECT_REL (1 << 6) /**< Section holds relocation recs. */ |
---|
97 | #define RTEMS_RTL_OBJ_SECT_RELA (1 << 7) /**< Section holds reloc addend recs. */ |
---|
98 | #define RTEMS_RTL_OBJ_SECT_SYM (1 << 8) /**< Section holds symbols. */ |
---|
99 | #define RTEMS_RTL_OBJ_SECT_STR (1 << 9) /**< Section holds strings. */ |
---|
100 | #define RTEMS_RTL_OBJ_SECT_ALLOC (1 << 10 /**< Section allocates runtime memory. */ |
---|
101 | #define RTEMS_RTL_OBJ_SECT_LOAD (1 << 11) /**< Section is loaded from object file. */ |
---|
102 | #define RTEMS_RTL_OBJ_SECT_WRITE (1 << 12) /**< Section is writable, ie data. */ |
---|
103 | #define RTEMS_RTL_OBJ_SECT_EXEC (1 << 13) /**< Section is executable. */ |
---|
104 | #define RTEMS_RTL_OBJ_SECT_ZERO (1 << 14) /**< Section is preset to zero. */ |
---|
105 | #define RTEMS_RTL_OBJ_SECT_LINK (1 << 15) /**< Section is link-ordered. */ |
---|
106 | #define RTEMS_RTL_OBJ_SECT_CTOR (1 << 16) /**< Section contains constructors. */ |
---|
107 | #define RTEMS_RTL_OBJ_SECT_DTOR (1 << 17) /**< Section contains destructors. */ |
---|
108 | #define RTEMS_RTL_OBJ_SECT_LOCD (1 << 18) /**< Section has been located. */ |
---|
109 | #define RTEMS_RTL_OBJ_SECT_ARCH_ALLOC (1 << 19) /**< Section use arch allocator. */ |
---|
110 | |
---|
111 | /** |
---|
112 | * Section types mask. |
---|
113 | */ |
---|
114 | #define RTEMS_RTL_OBJ_SECT_TYPES (RTEMS_RTL_OBJ_SECT_TEXT | \ |
---|
115 | RTEMS_RTL_OBJ_SECT_CONST | \ |
---|
116 | RTEMS_RTL_OBJ_SECT_DATA | \ |
---|
117 | RTEMS_RTL_OBJ_SECT_BSS | \ |
---|
118 | RTEMS_RTL_OBJ_SECT_TLS | \ |
---|
119 | RTEMS_RTL_OBJ_SECT_EH) |
---|
120 | |
---|
121 | /** |
---|
122 | * An object file is made up of sections and the can be more than |
---|
123 | * one of a specific type of sections. All sections and grouped |
---|
124 | * together in memory. |
---|
125 | */ |
---|
126 | struct rtems_rtl_obj_sect |
---|
127 | { |
---|
128 | rtems_chain_node node; /**< The node's link in the chain. */ |
---|
129 | int section; /**< The section number. */ |
---|
130 | const char* name; /**< The section's name. */ |
---|
131 | size_t size; /**< The size of the section in memory. */ |
---|
132 | off_t offset; /**< Offset into the object file. Relative to |
---|
133 | * the start of the object file. */ |
---|
134 | uint32_t alignment; /**< Alignment of this section. */ |
---|
135 | int link; /**< Section link field. */ |
---|
136 | int info; /**< Secfion info field. */ |
---|
137 | uint32_t flags; /**< The section's flags. */ |
---|
138 | void* base; /**< The base address of the section in |
---|
139 | * memory. */ |
---|
140 | int load_order; /**< Order we load sections. */ |
---|
141 | }; |
---|
142 | |
---|
143 | /** |
---|
144 | * Object file dependents. This is a list of tables of pointers to the object |
---|
145 | * modules the object file depends on. The table is a list of tables because |
---|
146 | * unresolved externals can exist when an object file is loaded and resolved |
---|
147 | * later when the dependent object file is loaded. |
---|
148 | */ |
---|
149 | struct rtems_rtl_obj_depends |
---|
150 | { |
---|
151 | rtems_chain_node node; /**< The node's link in the chain. */ |
---|
152 | size_t dependents; /**< The number of dependent object pointers. */ |
---|
153 | rtems_rtl_obj* depends[]; /**< Dependtent objects. More follow. */ |
---|
154 | }; |
---|
155 | |
---|
156 | /** |
---|
157 | * Dependency iterator. |
---|
158 | */ |
---|
159 | typedef bool (*rtems_rtl_obj_depends_iterator) (rtems_rtl_obj* obj, |
---|
160 | rtems_rtl_obj* dependent, |
---|
161 | void* data); |
---|
162 | |
---|
163 | /** |
---|
164 | * Object file descriptor flags. |
---|
165 | */ |
---|
166 | #define RTEMS_RTL_OBJ_LOCKED (1 << 0) /**< Lock the object file so it cannot |
---|
167 | * be unloaded. */ |
---|
168 | #define RTEMS_RTL_OBJ_UNRESOLVED (1 << 1) /**< The object file has unresolved |
---|
169 | * external symbols. */ |
---|
170 | #define RTEMS_RTL_OBJ_BASE (1 << 2) /**< The base image. */ |
---|
171 | #define RTEMS_RTL_OBJ_RELOC_TAG (1 << 3) /**< Tag the object as visited when reloc |
---|
172 | * parsing. */ |
---|
173 | #define RTEMS_RTL_OBJ_DEP_VISITED (1 << 4) /**< Dependency loop detection. */ |
---|
174 | #define RTEMS_RTL_OBJ_CTOR_RUN (1 << 5) /**< Constructors have been called. */ |
---|
175 | |
---|
176 | /** |
---|
177 | * RTL Object. There is one for each object module loaded plus one for the base |
---|
178 | * kernel image. |
---|
179 | */ |
---|
180 | struct rtems_rtl_obj |
---|
181 | { |
---|
182 | rtems_chain_node link; /**< The node's link in the chain. */ |
---|
183 | uint32_t flags; /**< The status of the object file. */ |
---|
184 | size_t users; /**< Users of this object file, number of loads. */ |
---|
185 | size_t refs; /**< References to the object file. */ |
---|
186 | int format; /**< The format of the object file. */ |
---|
187 | const char* fname; /**< The file name for the object. */ |
---|
188 | const char* oname; /**< The object file name. Can be |
---|
189 | * relative. */ |
---|
190 | const char* aname; /**< The archive name containing the |
---|
191 | * object. NULL means the object is not |
---|
192 | * in a lib */ |
---|
193 | off_t ooffset; /**< The object offset in the archive. */ |
---|
194 | size_t fsize; /**< Size of the object file. */ |
---|
195 | rtems_chain_control sections; /**< The sections of interest in the object |
---|
196 | * file. */ |
---|
197 | rtems_chain_control dependents; /**< The dependent object files. */ |
---|
198 | rtems_rtl_obj_sym* local_table; /**< Local symbol table. */ |
---|
199 | size_t local_syms; /**< Local symbol count. */ |
---|
200 | size_t local_size; /**< Local symbol memory usage. */ |
---|
201 | rtems_rtl_obj_sym* global_table; /**< Global symbol table. */ |
---|
202 | size_t global_syms; /**< Global symbol count. */ |
---|
203 | size_t global_size; /**< Global symbol memory usage. */ |
---|
204 | size_t unresolved; /**< The number of unresolved relocations. */ |
---|
205 | void* text_base; /**< The base address of the text section |
---|
206 | * in memory. */ |
---|
207 | size_t text_size; /**< The size of the text section. */ |
---|
208 | void* const_base; /**< The base address of the const section |
---|
209 | * in memory. */ |
---|
210 | size_t const_size; /**< The size of the const section. */ |
---|
211 | void* eh_base; /**< The base address of the eh section in |
---|
212 | * memory. */ |
---|
213 | size_t eh_size; /**< The size of the eh section. */ |
---|
214 | void* data_base; /**< The base address of the data section |
---|
215 | * in memory. */ |
---|
216 | size_t data_size; /**< The size of the data section. */ |
---|
217 | void* bss_base; /**< The base address of the bss section in |
---|
218 | * memory. */ |
---|
219 | size_t bss_size; /**< The size of the bss section. */ |
---|
220 | size_t exec_size; /**< The amount of executable memory |
---|
221 | * allocated */ |
---|
222 | void* entry; /**< The entry point of the module. */ |
---|
223 | uint32_t checksum; /**< The checksum of the text sections. A |
---|
224 | * zero means do not checksum. */ |
---|
225 | uint32_t* sec_num; /**< The sec nums of each obj. */ |
---|
226 | uint32_t obj_num; /**< The count of elf files in an rtl |
---|
227 | * obj. */ |
---|
228 | void* trampoline; /**< Trampoline memory. Used for fixups or |
---|
229 | * veneers */ |
---|
230 | size_t tramp_size; /**< Size of a tramopline slot. */ |
---|
231 | size_t tramps_size; /**< Size of the trampoline memory. */ |
---|
232 | void* tramp_brk; /**< Trampoline memory allocator. MD |
---|
233 | * relocators can take memory from the |
---|
234 | * break up to the size. */ |
---|
235 | size_t tramp_relocs; /**< Number of slots reserved for |
---|
236 | * relocs. The remainder are for |
---|
237 | * unresolved symbols. */ |
---|
238 | struct link_map* linkmap; /**< For GDB. */ |
---|
239 | void* loader; /**< The file details specific to a |
---|
240 | * loader. */ |
---|
241 | }; |
---|
242 | |
---|
243 | /** |
---|
244 | * A section handler is called once for each section that needs to be |
---|
245 | * processed by this handler. The handler is specific to a task. |
---|
246 | * |
---|
247 | * @param obj The object file's descriptor the section belongs too. |
---|
248 | * @param fd The file descriptor of the object file beling loaded. |
---|
249 | * @param sect The section the handler is being invoked to handle. |
---|
250 | * @param data A user supplied data variable. |
---|
251 | * @retval true The operation was successful. |
---|
252 | * @retval false The operation failed and the RTL has been set. |
---|
253 | */ |
---|
254 | typedef bool (*rtems_rtl_obj_sect_handler)(rtems_rtl_obj* obj, |
---|
255 | int fd, |
---|
256 | rtems_rtl_obj_sect* sect, |
---|
257 | void* data); |
---|
258 | |
---|
259 | /** |
---|
260 | * Get the file name. |
---|
261 | * |
---|
262 | * @param obj The object file. |
---|
263 | * @return const char* The string. |
---|
264 | */ |
---|
265 | static inline const char* rtems_rtl_obj_fname (const rtems_rtl_obj* obj) |
---|
266 | { |
---|
267 | return obj->fname; |
---|
268 | } |
---|
269 | |
---|
270 | /** |
---|
271 | * Is the file name valid ? |
---|
272 | * |
---|
273 | * @param obj The object file. |
---|
274 | * @return bool There is a file name |
---|
275 | */ |
---|
276 | static inline bool rtems_rtl_obj_fname_valid (const rtems_rtl_obj* obj) |
---|
277 | { |
---|
278 | return obj->fname; |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | * Get the object name. |
---|
283 | * |
---|
284 | * @param obj The object file. |
---|
285 | * @return const char* The string. |
---|
286 | */ |
---|
287 | static inline const char* rtems_rtl_obj_oname (const rtems_rtl_obj* obj) |
---|
288 | { |
---|
289 | return obj->oname; |
---|
290 | } |
---|
291 | |
---|
292 | /** |
---|
293 | * Is the object name valid ? |
---|
294 | * |
---|
295 | * @param obj The object file. |
---|
296 | * @return bool There is an object name |
---|
297 | */ |
---|
298 | static inline bool rtems_rtl_obj_oname_valid (const rtems_rtl_obj* obj) |
---|
299 | { |
---|
300 | return obj->oname; |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | * Get the archive name. |
---|
305 | * |
---|
306 | * @param obj The object file. |
---|
307 | * @return const char* The string. |
---|
308 | */ |
---|
309 | static inline const char* rtems_rtl_obj_aname (const rtems_rtl_obj* obj) |
---|
310 | { |
---|
311 | return obj->aname; |
---|
312 | } |
---|
313 | |
---|
314 | /** |
---|
315 | * Is the archive name valid ? |
---|
316 | * |
---|
317 | * @param obj The object file. |
---|
318 | * @return bool There is an archive name |
---|
319 | */ |
---|
320 | static inline bool rtems_rtl_obj_aname_valid (const rtems_rtl_obj* obj) |
---|
321 | { |
---|
322 | return obj->aname; |
---|
323 | } |
---|
324 | |
---|
325 | /** |
---|
326 | * Is the address inside the text section? |
---|
327 | * |
---|
328 | * @param obj The object file. |
---|
329 | * @return bool There is an archive name |
---|
330 | */ |
---|
331 | static inline bool rtems_rtl_obj_text_inside (const rtems_rtl_obj* obj, |
---|
332 | const void* address) |
---|
333 | { |
---|
334 | return |
---|
335 | (address >= obj->text_base) && |
---|
336 | ((char*) address < ((char*) obj->text_base + obj->text_size)); |
---|
337 | } |
---|
338 | |
---|
339 | /** |
---|
340 | * Align the size to the next alignment point. Assume the alignment is a |
---|
341 | * positive integral power of 2 if not 0 or 1. If 0 or 1 then there is no |
---|
342 | * alignment. |
---|
343 | * |
---|
344 | * @param offset Offset to align up from. |
---|
345 | * @param alignment The alignment. |
---|
346 | * @return size_t Aligned offset. |
---|
347 | */ |
---|
348 | static inline size_t rtems_rtl_obj_align (size_t offset, |
---|
349 | uint32_t alignment) |
---|
350 | { |
---|
351 | if ((alignment > 1) && ((offset & (alignment - 1)) != 0)) |
---|
352 | offset = (offset + alignment) & ~(alignment - 1); |
---|
353 | return offset; |
---|
354 | } |
---|
355 | |
---|
356 | /** |
---|
357 | * Is the symbol in this object's files globa symbol table? |
---|
358 | * |
---|
359 | * @param obj The object file's descriptor to search. |
---|
360 | * @param sym The symbol to check. |
---|
361 | * @retval bool Returns @true if present else @false is returned. |
---|
362 | */ |
---|
363 | static inline bool rtems_rtl_obj_has_symbol (const rtems_rtl_obj* obj, |
---|
364 | const rtems_rtl_obj_sym* sym) |
---|
365 | { |
---|
366 | return (sym >= obj->global_table && |
---|
367 | sym < (obj->global_table + obj->global_syms)); |
---|
368 | } |
---|
369 | |
---|
370 | /** |
---|
371 | * Is there space in the trampoline memory for a trapoline. |
---|
372 | * |
---|
373 | * @param obj The object file's descriptor to check for available space. |
---|
374 | * @param size The size to be allocated. |
---|
375 | * @retval bool Returns @true if the space is available. |
---|
376 | */ |
---|
377 | static inline size_t rtems_rtl_obj_tramp_avail_space (const rtems_rtl_obj* obj) |
---|
378 | { |
---|
379 | return (char*) obj->tramp_brk - (char*) obj->trampoline; |
---|
380 | } |
---|
381 | |
---|
382 | /** |
---|
383 | * Is there space in the trampoline memory for a trapoline. |
---|
384 | * |
---|
385 | * @param obj The object file's descriptor to check for available space. |
---|
386 | * @param size The size to be allocated. |
---|
387 | * @retval bool Returns @true if the space is available. |
---|
388 | */ |
---|
389 | static inline bool rtems_rtl_obj_has_tramp_space (const rtems_rtl_obj* obj, |
---|
390 | const size_t size) |
---|
391 | { |
---|
392 | return (obj->trampoline != NULL && |
---|
393 | (rtems_rtl_obj_tramp_avail_space (obj) + size) <= obj->tramps_size); |
---|
394 | } |
---|
395 | |
---|
396 | /** |
---|
397 | * Trampoline slots. |
---|
398 | * |
---|
399 | * @param obj The object file's descriptor. |
---|
400 | * @retval size_t The number of trampoline slots. |
---|
401 | */ |
---|
402 | static inline size_t rtems_rtl_obj_trampoline_slots (const rtems_rtl_obj* obj) |
---|
403 | { |
---|
404 | return obj->trampoline == NULL || obj->tramp_size == 0 ? |
---|
405 | 0 : obj->tramps_size / obj->tramp_size; |
---|
406 | } |
---|
407 | |
---|
408 | /** |
---|
409 | * Number of trampolines. |
---|
410 | * |
---|
411 | * @param obj The object file's descriptor. |
---|
412 | * @retval size_t The number of trampolines. |
---|
413 | */ |
---|
414 | static inline size_t rtems_rtl_obj_trampolines (const rtems_rtl_obj* obj) |
---|
415 | { |
---|
416 | return obj->trampoline == NULL || obj->tramp_size == 0 ? |
---|
417 | 0 : rtems_rtl_obj_tramp_avail_space (obj) / obj->tramp_size; |
---|
418 | } |
---|
419 | |
---|
420 | /** |
---|
421 | * Does the section require architecture specific allocations? |
---|
422 | * |
---|
423 | * @param sect The section. |
---|
424 | * @retval bool Returns @true if the section requires arch allocation. |
---|
425 | */ |
---|
426 | static inline bool rtems_rtl_obj_sect_is_arch_alloc (rtems_rtl_obj_sect* sect) |
---|
427 | { |
---|
428 | return (sect->flags & RTEMS_RTL_OBJ_SECT_ARCH_ALLOC) != 0; |
---|
429 | } |
---|
430 | |
---|
431 | /** |
---|
432 | * Allocate an object structure on the heap. |
---|
433 | * |
---|
434 | * @retval NULL No memory for the object. |
---|
435 | */ |
---|
436 | rtems_rtl_obj* rtems_rtl_obj_alloc (void); |
---|
437 | |
---|
438 | /** |
---|
439 | * Free the object structure and related resources. |
---|
440 | * |
---|
441 | * @param obj The object file's descriptor to free. |
---|
442 | * @retval false The object has dependences. |
---|
443 | * @retval true The object has been freed. |
---|
444 | */ |
---|
445 | bool rtems_rtl_obj_free (rtems_rtl_obj* obj); |
---|
446 | |
---|
447 | /** |
---|
448 | * Does the object file have unresolved external references ? If it does the |
---|
449 | * results of executing code is unpredictable. |
---|
450 | * |
---|
451 | * @param obj The object file's descriptor. |
---|
452 | * @retval true The object file has unresolved externals. |
---|
453 | * @retval false The object file has all external references resolved. |
---|
454 | */ |
---|
455 | bool rtems_rtl_obj_unresolved (rtems_rtl_obj* obj); |
---|
456 | |
---|
457 | /** |
---|
458 | * Parses a filename and returns newly allocated strings with the archive name, |
---|
459 | * object name, and the object's offset |
---|
460 | * |
---|
461 | * @param name The filename of the object |
---|
462 | * @param aname Address of a string pointer that holds the archive name |
---|
463 | * @param oname Address of a string pointer that holds the object name |
---|
464 | * @param ooffset Address of an int that holds the object offset |
---|
465 | * @retval true The parsing was successful |
---|
466 | * @retval false The parsing was unsuccessful |
---|
467 | */ |
---|
468 | bool rtems_rtl_parse_name (const char* name, |
---|
469 | const char** aname, |
---|
470 | const char** oname, |
---|
471 | off_t* ooffset); |
---|
472 | |
---|
473 | /** |
---|
474 | * Find an object file on disk that matches the name. The object descriptor is |
---|
475 | * fill in with the various parts of a name. A name can have archive, object |
---|
476 | * file and offset components. The search path in the RTL is searched. |
---|
477 | * |
---|
478 | * @param obj The object file's descriptor. |
---|
479 | * @param name The name to locate on disk. |
---|
480 | * @retval true The file has been found. |
---|
481 | * @retval false The file could not be located. The RTL error has been set. |
---|
482 | */ |
---|
483 | bool rtems_rtl_obj_find_file (rtems_rtl_obj* obj, const char* name); |
---|
484 | |
---|
485 | /** |
---|
486 | * Add a section to the object descriptor. |
---|
487 | * |
---|
488 | * @param obj The object file's descriptor. |
---|
489 | * @param section The section's index number. |
---|
490 | * @param name The name of the section. |
---|
491 | * @param size The size of the section in memory. |
---|
492 | * @param offset The offset of the section in the object file. |
---|
493 | * @param alignment The alignment of the section in memory. |
---|
494 | * @param link The section's link field (from the ELF format). |
---|
495 | * @param info The section's info field (from the ELF format). |
---|
496 | * @param flags The section's flags. |
---|
497 | * @retval true The section has been added. |
---|
498 | * @retval false The section has not been added. See the RTL error. |
---|
499 | */ |
---|
500 | bool rtems_rtl_obj_add_section (rtems_rtl_obj* obj, |
---|
501 | int section, |
---|
502 | const char* name, |
---|
503 | size_t size, |
---|
504 | off_t offset, |
---|
505 | uint32_t alignment, |
---|
506 | int link, |
---|
507 | int info, |
---|
508 | uint32_t flags); |
---|
509 | |
---|
510 | /** |
---|
511 | * Erase the object file descriptor's sections. |
---|
512 | * |
---|
513 | * @param obj The object file's descriptor. |
---|
514 | */ |
---|
515 | void rtems_rtl_obj_erase_sections (rtems_rtl_obj* obj); |
---|
516 | |
---|
517 | /** |
---|
518 | * Find the section given a name. |
---|
519 | * |
---|
520 | * @param obj The object file's descriptor. |
---|
521 | * @param name The name of the section to find. |
---|
522 | * @retval NULL The section was not found. |
---|
523 | * @return rtems_rtl_obj_sect_t* The named section. |
---|
524 | */ |
---|
525 | rtems_rtl_obj_sect* rtems_rtl_obj_find_section (const rtems_rtl_obj* obj, |
---|
526 | const char* name); |
---|
527 | |
---|
528 | /** |
---|
529 | * Find a section given a section's index number. |
---|
530 | * |
---|
531 | * @param obj The object file's descriptor. |
---|
532 | * @param index The section's index to find. |
---|
533 | * @retval NULL The section was not found. |
---|
534 | * @return rtems_rtl_obj_sect_t* The found section. |
---|
535 | */ |
---|
536 | rtems_rtl_obj_sect* rtems_rtl_obj_find_section_by_index (const rtems_rtl_obj* obj, |
---|
537 | int index); |
---|
538 | |
---|
539 | /** |
---|
540 | * Find a section given a section's mask. The index is the section after which |
---|
541 | * the mask is matched. An index of -1 starts the search from the beginning of |
---|
542 | * the section list. You can find multiple matches for a mask by passing the |
---|
543 | * index of the last section that matched the mask on a subsequent call. |
---|
544 | * |
---|
545 | * @param obj The object file's descriptor. |
---|
546 | * @param index The section's index to start searching from, -1 for the start. |
---|
547 | * @param mask The section's mask to match against the section's flags. |
---|
548 | * @retval NULL The section was not found. |
---|
549 | * @return rtems_rtl_obj_sect_t* The found section. |
---|
550 | */ |
---|
551 | rtems_rtl_obj_sect* rtems_rtl_obj_find_section_by_mask (const rtems_rtl_obj* obj, |
---|
552 | int index, |
---|
553 | uint32_t mask); |
---|
554 | |
---|
555 | /** |
---|
556 | * Allocate a table for trampoline fixup calls. |
---|
557 | * |
---|
558 | * @param obj The object file's descriptor. |
---|
559 | * @retval true The table was allocated. |
---|
560 | * @retval false The alloction failed. |
---|
561 | */ |
---|
562 | bool rtems_rtl_obj_alloc_trampoline (rtems_rtl_obj* obj); |
---|
563 | |
---|
564 | /** |
---|
565 | * Erase the object file descriptor's trampoline table.. |
---|
566 | * |
---|
567 | * @param obj The object file's descriptor. |
---|
568 | */ |
---|
569 | void rtems_rtl_obj_erase_trampoline (rtems_rtl_obj* obj); |
---|
570 | |
---|
571 | /** |
---|
572 | * Allocate a table for dependent objects. |
---|
573 | * |
---|
574 | * @param obj The object file's descriptor. |
---|
575 | * @param dependents The size of the table. |
---|
576 | * @retval true The table was allocated. |
---|
577 | * @retval false The alloction failed. |
---|
578 | */ |
---|
579 | bool rtems_rtl_obj_alloc_dependents (rtems_rtl_obj* obj, size_t dependents); |
---|
580 | |
---|
581 | /** |
---|
582 | * Erase the object file descriptor's dependents. |
---|
583 | * |
---|
584 | * @param obj The object file's descriptor. |
---|
585 | */ |
---|
586 | void rtems_rtl_obj_erase_dependents (rtems_rtl_obj* obj); |
---|
587 | |
---|
588 | /** |
---|
589 | * Add an object file to the dependents table. |
---|
590 | * |
---|
591 | * @param obj The object file's descriptor. |
---|
592 | * @param dependent The dependent object file to add. |
---|
593 | * @retval true The dependent has been added to the table. |
---|
594 | * @retval false There is no space in the table. |
---|
595 | */ |
---|
596 | bool rtems_rtl_obj_add_dependent (rtems_rtl_obj* obj, rtems_rtl_obj* dependent); |
---|
597 | |
---|
598 | /** |
---|
599 | * Remove dependencies. This decrements the dependent object file references. |
---|
600 | * |
---|
601 | * @param obj The object file's descriptor. |
---|
602 | * @retval true The dependencies have been removed. |
---|
603 | * @retval false There is no space in the table. |
---|
604 | */ |
---|
605 | bool rtems_rtl_obj_remove_dependencies (rtems_rtl_obj* obj); |
---|
606 | |
---|
607 | /** |
---|
608 | * Iterate over the module dependenices. |
---|
609 | * |
---|
610 | * @param obj The object file's descriptor. |
---|
611 | * @param handler The iterator handler. Returns true to end. |
---|
612 | * @param data User data passed to the iterator. |
---|
613 | * @retval true The iterator handler returned true. |
---|
614 | * @retval false The iterator handler returned false. |
---|
615 | */ |
---|
616 | bool rtems_rtl_obj_iterate_dependents (rtems_rtl_obj* obj, |
---|
617 | rtems_rtl_obj_depends_iterator iterator, |
---|
618 | void* data); |
---|
619 | |
---|
620 | /** |
---|
621 | * The text section size. Only use once all the sections has been added. It |
---|
622 | * includes alignments between sections that are part of the object's text |
---|
623 | * area. The consts sections are included in this section. |
---|
624 | * |
---|
625 | * @param obj The object file's descriptor. |
---|
626 | * @return size_t The size of the text area of the object file. |
---|
627 | */ |
---|
628 | size_t rtems_rtl_obj_text_size (const rtems_rtl_obj* obj); |
---|
629 | |
---|
630 | /** |
---|
631 | * The text section alignment for the object file. Only use once all the |
---|
632 | * sections has been added. The section alignment is the alignment of the first |
---|
633 | * text type section loaded the text section. |
---|
634 | * |
---|
635 | * You can assume the alignment is a positive integral power of 2 if not 0 or |
---|
636 | * 1. If 0 or 1 then there is no alignment. |
---|
637 | * |
---|
638 | * @param obj The object file's descriptor. |
---|
639 | * @return uint32_t The alignment. Can be 0 or 1 for not aligned or the alignment. |
---|
640 | */ |
---|
641 | uint32_t rtems_rtl_obj_text_alignment (const rtems_rtl_obj* obj); |
---|
642 | |
---|
643 | /** |
---|
644 | * The const section size. Only use once all the sections has been added. It |
---|
645 | * includes alignments between sections that are part of the object's const |
---|
646 | * area. The consts sections are included in this section. |
---|
647 | * |
---|
648 | * @param obj The object file's descriptor. |
---|
649 | * @return size_t The size of the const area of the object file. |
---|
650 | */ |
---|
651 | size_t rtems_rtl_obj_const_size (const rtems_rtl_obj* obj); |
---|
652 | |
---|
653 | /** |
---|
654 | * The const section alignment for the object file. Only use once all the |
---|
655 | * sections has been added. The section alignment is the alignment of the first |
---|
656 | * const type section loaded the const section. |
---|
657 | * |
---|
658 | * You can assume the alignment is a positive integral power of 2 if not 0 or |
---|
659 | * 1. If 0 or 1 then there is no alignment. |
---|
660 | * |
---|
661 | * @param obj The object file's descriptor. |
---|
662 | * @return uint32_t The alignment. Can be 0 or 1 for not aligned or the alignment. |
---|
663 | */ |
---|
664 | uint32_t rtems_rtl_obj_const_alignment (const rtems_rtl_obj* obj); |
---|
665 | |
---|
666 | /** |
---|
667 | * The eh section size. Only use once all the sections has been added. It |
---|
668 | * includes alignments between sections that are part of the object's bss area. |
---|
669 | * |
---|
670 | * @param obj The object file's descriptor. |
---|
671 | * @return size_t The size of the bss area of the object file. |
---|
672 | */ |
---|
673 | size_t rtems_rtl_obj_eh_size (const rtems_rtl_obj* obj); |
---|
674 | |
---|
675 | /** |
---|
676 | * The eh section alignment for the object file. Only use once all the sections |
---|
677 | * has been added. The section alignment is the alignment of the first bss type |
---|
678 | * section loaded the bss section. |
---|
679 | * |
---|
680 | * You can assume the alignment is a positive integral power of 2 if not 0 or |
---|
681 | * 1. If 0 or 1 then there is no alignment. |
---|
682 | * |
---|
683 | * @param obj The object file's descriptor. |
---|
684 | * @return uint32_t The alignment. Can be 0 or 1 for not aligned or the alignment. |
---|
685 | */ |
---|
686 | uint32_t rtems_rtl_obj_eh_alignment (const rtems_rtl_obj* obj); |
---|
687 | |
---|
688 | /** |
---|
689 | * The data section size. Only use once all the sections has been added. It |
---|
690 | * includes alignments between sections that are part of the object's data |
---|
691 | * area. |
---|
692 | * |
---|
693 | * @param obj The object file's descriptor. |
---|
694 | * @return size_t The size of the data area of the object file. |
---|
695 | */ |
---|
696 | size_t rtems_rtl_obj_data_size (const rtems_rtl_obj* obj); |
---|
697 | |
---|
698 | /** |
---|
699 | * The data section alignment for the object file. Only use once all the |
---|
700 | * sections has been added. The section alignment is the alignment of the first |
---|
701 | * data type section loaded the data section. |
---|
702 | * |
---|
703 | * You can assume the alignment is a positive integral power of 2 if not 0 or |
---|
704 | * 1. If 0 or 1 then there is no alignment. |
---|
705 | * |
---|
706 | * @param obj The object file's descriptor. |
---|
707 | * @return uint32_t The alignment. Can be 0 or 1 for not aligned or the alignment. |
---|
708 | */ |
---|
709 | uint32_t rtems_rtl_obj_data_alignment (const rtems_rtl_obj* obj); |
---|
710 | |
---|
711 | /** |
---|
712 | * The bss section size. Only use once all the sections has been added. It |
---|
713 | * includes alignments between sections that are part of the object's bss area. |
---|
714 | * |
---|
715 | * @param obj The object file's descriptor. |
---|
716 | * @return size_t The size of the bss area of the object file. |
---|
717 | */ |
---|
718 | size_t rtems_rtl_obj_bss_size (const rtems_rtl_obj* obj); |
---|
719 | |
---|
720 | /** |
---|
721 | * The bss section alignment for the object file. Only use once all the |
---|
722 | * sections has been added. The section alignment is the alignment of the first |
---|
723 | * bss type section loaded the bss section. |
---|
724 | * |
---|
725 | * You can assume the alignment is a positive integral power of 2 if not 0 or |
---|
726 | * 1. If 0 or 1 then there is no alignment. |
---|
727 | * |
---|
728 | * @param obj The object file's descriptor. |
---|
729 | * @return uint32_t The alignment. Can be 0 or 1 for not aligned or the alignment. |
---|
730 | */ |
---|
731 | uint32_t rtems_rtl_obj_bss_alignment (const rtems_rtl_obj* obj); |
---|
732 | |
---|
733 | /** |
---|
734 | * Relocate the object file. The object file's section are parsed for any |
---|
735 | * relocation type sections. |
---|
736 | * |
---|
737 | * @param obj The object file's descriptor. |
---|
738 | * @param fd The object file's file descriptor. |
---|
739 | * @param handler The object file's format specific relocation handler. |
---|
740 | * @param data User specific data handle. |
---|
741 | * @retval true The object file was relocated. |
---|
742 | * @retval false The relocation failed. The RTL error is set. |
---|
743 | */ |
---|
744 | bool rtems_rtl_obj_relocate (rtems_rtl_obj* obj, |
---|
745 | int fd, |
---|
746 | rtems_rtl_obj_sect_handler handler, |
---|
747 | void* data); |
---|
748 | |
---|
749 | /** |
---|
750 | * Synchronize caches to make code visible to CPU(s) |
---|
751 | * |
---|
752 | * @param obj The object file's descriptor. |
---|
753 | */ |
---|
754 | void rtems_rtl_obj_synchronize_cache (rtems_rtl_obj* obj); |
---|
755 | |
---|
756 | /** |
---|
757 | * Relocate an object file's unresolved reference. |
---|
758 | * |
---|
759 | * @param rec The unresolved relocation record. |
---|
760 | * @param sym The unresolved relocation's referenced symbol. |
---|
761 | * @retval true The object file record was relocated. |
---|
762 | * @retval false The relocation failed. The RTL error is set. |
---|
763 | */ |
---|
764 | bool rtems_rtl_obj_relocate_unresolved (rtems_rtl_unresolv_reloc* reloc, |
---|
765 | rtems_rtl_obj_sym* sym); |
---|
766 | |
---|
767 | /** |
---|
768 | * Load the symbols from the object file. Only the exported or public symbols |
---|
769 | * are read into memory and held in the global symbol table. |
---|
770 | * |
---|
771 | * @param obj The object file's descriptor. |
---|
772 | * @param fd The object file's file descriptor. |
---|
773 | * @param handler The object file's format specific symbol handler. |
---|
774 | * @param data User specific data handle. |
---|
775 | * @retval true The object file's symbol where loaded. |
---|
776 | * @retval false The symbol loading failed. The RTL error is set. |
---|
777 | */ |
---|
778 | bool rtems_rtl_obj_load_symbols (rtems_rtl_obj* obj, |
---|
779 | int fd, |
---|
780 | rtems_rtl_obj_sect_handler handler, |
---|
781 | void* data); |
---|
782 | |
---|
783 | /** |
---|
784 | * Allocate the sections. If a handler is provided (not NULL) it is called for |
---|
785 | * all section. |
---|
786 | * |
---|
787 | * @param obj The object file's descriptor. |
---|
788 | * @param fd The object file's file descriptor. |
---|
789 | * @param handler The object file's format specific allocation handler. |
---|
790 | * @param data User specific data handle. |
---|
791 | * @retval true The object has been sucessfully loaded. |
---|
792 | * @retval false The load failed. The RTL error has been set. |
---|
793 | */ |
---|
794 | bool |
---|
795 | rtems_rtl_obj_alloc_sections (rtems_rtl_obj* obj, |
---|
796 | int fd, |
---|
797 | rtems_rtl_obj_sect_handler handler, |
---|
798 | void* data); |
---|
799 | |
---|
800 | /** |
---|
801 | * Load the sections that have been allocated memory in the target. The bss |
---|
802 | * type section does not load any data, it is set to 0. The text and data |
---|
803 | * sections read the detault data from the object file into the target memory. |
---|
804 | * |
---|
805 | * @param obj The object file's descriptor. |
---|
806 | * @param fd The object file's file descriptor. |
---|
807 | * @param handler The object file's format specific load handler. |
---|
808 | * @param data User specific data handle. |
---|
809 | * @retval true The object has been sucessfully loaded. |
---|
810 | * @retval false The load failed. The RTL error has been set. |
---|
811 | */ |
---|
812 | bool rtems_rtl_obj_load_sections (rtems_rtl_obj* obj, |
---|
813 | int fd, |
---|
814 | rtems_rtl_obj_sect_handler handler, |
---|
815 | void* data); |
---|
816 | |
---|
817 | /** |
---|
818 | * Does the object have constructors to run? |
---|
819 | * |
---|
820 | * @return bool True if there are constructors to run. |
---|
821 | */ |
---|
822 | bool rtems_rtl_obj_ctors_to_run (rtems_rtl_obj* obj); |
---|
823 | |
---|
824 | /** |
---|
825 | * Invoke the constructors the object has. Constructors are a table of pointers |
---|
826 | * to "void (*)(void);" where NULL pointers are skipped. The table's size is |
---|
827 | * taken from the section's size. The objet ELF specific code is responisble |
---|
828 | * for flagging which sections contain constructors. |
---|
829 | * |
---|
830 | * @param obj The object file's descriptor. |
---|
831 | */ |
---|
832 | void rtems_rtl_obj_run_ctors (rtems_rtl_obj* obj); |
---|
833 | |
---|
834 | /** |
---|
835 | * Does the object have destructors to run? |
---|
836 | * |
---|
837 | * @return bool True if there are destructors to run. |
---|
838 | */ |
---|
839 | bool rtems_rtl_obj_dtors_to_run (rtems_rtl_obj* obj); |
---|
840 | |
---|
841 | /** |
---|
842 | * Invoke the destructors the object has. Destructors are a table of pointers |
---|
843 | * to "void (*)(void);" where NULL pointers are skipped. The table's size is |
---|
844 | * taken from the section's size. The objet ELF specific code is responisble |
---|
845 | * for flagging which sections contain destructors. |
---|
846 | * |
---|
847 | * @param obj The object file's descriptor. |
---|
848 | */ |
---|
849 | void rtems_rtl_obj_run_dtors (rtems_rtl_obj* obj); |
---|
850 | |
---|
851 | /** |
---|
852 | * Get the object file reference count. |
---|
853 | * |
---|
854 | * @retval int The object file's reference count. |
---|
855 | */ |
---|
856 | size_t rtems_rtl_obj_get_reference (rtems_rtl_obj* obj); |
---|
857 | |
---|
858 | /** |
---|
859 | * Increment the object file reference count. |
---|
860 | * |
---|
861 | * @param obj The object file's descriptor. |
---|
862 | */ |
---|
863 | void rtems_rtl_obj_inc_reference (rtems_rtl_obj* obj); |
---|
864 | |
---|
865 | /** |
---|
866 | * Decrement the object file reference count. |
---|
867 | * |
---|
868 | * @param obj The object file's descriptor. |
---|
869 | */ |
---|
870 | void rtems_rtl_obj_dec_reference (rtems_rtl_obj* obj); |
---|
871 | |
---|
872 | /** |
---|
873 | * Is the object file orphaned? An orphaned object file is not locked, has no |
---|
874 | * users and it not being referenced. |
---|
875 | * |
---|
876 | * @param obj The object file's descriptor. |
---|
877 | */ |
---|
878 | bool rtems_rtl_obj_orphaned (rtems_rtl_obj* obj); |
---|
879 | |
---|
880 | /** |
---|
881 | * Load the object file, reading all sections into memory, symbols and |
---|
882 | * performing any relocation fixups. |
---|
883 | * |
---|
884 | * @param obj The object file's descriptor. |
---|
885 | * @retval true The object file has been loaded. |
---|
886 | * @retval false The load failed. The RTL error has been set. |
---|
887 | */ |
---|
888 | bool rtems_rtl_obj_load (rtems_rtl_obj* obj); |
---|
889 | |
---|
890 | /** |
---|
891 | * Unload the object file, erasing all symbols and releasing all memory. |
---|
892 | * |
---|
893 | * @param obj The object file's descriptor. |
---|
894 | * @retval true The object file has been unloaded. |
---|
895 | * @retval false The unload failed. The RTL error has been set. |
---|
896 | */ |
---|
897 | bool rtems_rtl_obj_unload (rtems_rtl_obj* obj); |
---|
898 | |
---|
899 | #ifdef __cplusplus |
---|
900 | } |
---|
901 | #endif /* __cplusplus */ |
---|
902 | |
---|
903 | #endif |
---|