| 1 |
/* SPDX-License-Identifier: BSD-2-Clause */ |
| 2 |
/* Copyright 1996-2025 The NASM Authors - All Rights Reserved */ |
| 3 |
|
| 4 |
#ifndef NASM_OUTLIB_H |
| 5 |
#define NASM_OUTLIB_H |
| 6 |
|
| 7 |
#include "compiler.h" |
| 8 |
#include "nasm.h" |
| 9 |
#include "error.h" |
| 10 |
#include "hashtbl.h" |
| 11 |
#include "saa.h" |
| 12 |
#include "rbtree.h" |
| 13 |
|
| 14 |
uint64_t realsize(enum out_type type, uint64_t size); |
| 15 |
|
| 16 |
/* Do-nothing versions of some output routines */ |
| 17 |
enum directive_result |
| 18 |
null_directive(enum directive directive, char *value); |
| 19 |
void null_sectalign(int32_t seg, unsigned int value); |
| 20 |
void null_reset(void); |
| 21 |
int32_t null_segbase(int32_t seg); |
| 22 |
|
| 23 |
/* Do-nothing versions of all the debug routines */ |
| 24 |
void null_debug_init(void); |
| 25 |
void null_debug_linenum(const char *filename, int32_t linenumber, |
| 26 |
int32_t segto); |
| 27 |
void null_debug_deflabel(char *name, int32_t segment, int64_t offset, |
| 28 |
int is_global, char *special); |
| 29 |
void null_debug_directive(const char *directive, const char *params); |
| 30 |
void null_debug_typevalue(int32_t type); |
| 31 |
void null_debug_output(int type, void *param); |
| 32 |
void null_debug_cleanup(void); |
| 33 |
extern const struct dfmt * const null_debug_arr[2]; |
| 34 |
|
| 35 |
/* |
| 36 |
* This macro expands the legacy output information into separate |
| 37 |
* variables, to make gradual porting of backends easier. |
| 38 |
*/ |
| 39 |
#define OUT_LEGACY(_out,_segto,_data,_type,_size,_segment,_wrt) \ |
| 40 |
int32_t _segto = (_out)->loc.segment; \ |
| 41 |
const void *_data = (_out)->legacy.data; \ |
| 42 |
enum out_type _type = (_out)->legacy.type; \ |
| 43 |
uint64_t _size = (_out)->legacy.size; \ |
| 44 |
int32_t _segment = (_out)->legacy.tsegment; \ |
| 45 |
int32_t _wrt = (_out)->legacy.twrt |
| 46 |
|
| 47 |
/* |
| 48 |
* Common routines for tasks that really should migrate into the core. |
| 49 |
* This provides a common interface for maintaining sections and symbols, |
| 50 |
* and provide quick lookups as well as declared-order sequential walks. |
| 51 |
* |
| 52 |
* These structures are intended to be embedded at the *top* of a |
| 53 |
* backend-specific structure containing additional information. |
| 54 |
* |
| 55 |
* The tokens O_Section, O_Symbol and O_Reloc are intended to be |
| 56 |
* defined as macros by the backend before including this file! |
| 57 |
*/ |
| 58 |
|
| 59 |
struct ol_sect; |
| 60 |
struct ol_sym; |
| 61 |
|
| 62 |
#ifndef O_Section |
| 63 |
typedef struct ol_sect O_Section; |
| 64 |
#endif |
| 65 |
#ifndef O_Symbol |
| 66 |
typedef struct ol_sym O_Symbol; |
| 67 |
#endif |
| 68 |
#ifndef O_Reloc |
| 69 |
typedef void * O_Reloc; |
| 70 |
#endif |
| 71 |
|
| 72 |
/* Common section structure */ |
| 73 |
|
| 74 |
/* |
| 75 |
* Common flags for sections and symbols; low values reserved for |
| 76 |
* backend. Note that both ol_sect and ol_sym begin with a flags |
| 77 |
* field, so if a section pointer points to an external symbol instead |
| 78 |
* they can be trivially resolved. |
| 79 |
*/ |
| 80 |
#define OF_SYMBOL 0x80000000 |
| 81 |
#define OF_GLOBAL 0x40000000 |
| 82 |
#define OF_IMPSEC 0x20000000 |
| 83 |
#define OF_COMMON 0x10000000 |
| 84 |
|
| 85 |
struct ol_sym; |
| 86 |
|
| 87 |
struct ol_symlist { |
| 88 |
struct ol_symlist *next; |
| 89 |
struct rbtree tree; |
| 90 |
}; |
| 91 |
struct ol_symhead { |
| 92 |
struct ol_symlist *head, **tail; |
| 93 |
struct rbtree *tree; |
| 94 |
uint64_t n; |
| 95 |
}; |
| 96 |
|
| 97 |
struct ol_sect { |
| 98 |
uint32_t flags; /* Section/symbol flags */ |
| 99 |
struct ol_sect *next; /* Next section in declared order */ |
| 100 |
const char *name; /* Name of section */ |
| 101 |
struct ol_symhead syml; /* All symbols in this section */ |
| 102 |
struct ol_symhead symg; /* Global symbols in this section */ |
| 103 |
struct SAA *data; /* Contents of section */ |
| 104 |
struct SAA *reloc; /* Section relocations */ |
| 105 |
uint32_t index; /* Primary section index */ |
| 106 |
uint32_t subindex; /* Current subsection index */ |
| 107 |
}; |
| 108 |
|
| 109 |
/* Segment reference */ |
| 110 |
enum ol_seg_type { |
| 111 |
OS_NOSEG = 0, /* Plain number (no segment) */ |
| 112 |
OS_SEGREF = 1, /* It is a segment reference */ |
| 113 |
OS_ABS = 1, /* Absolute segment reference */ |
| 114 |
OS_SECT = 2, /* It is a real section */ |
| 115 |
OS_OFFS = OS_SECT, /* Offset reference in section */ |
| 116 |
OS_SEG = OS_SECT|OS_SEGREF /* Section reference */ |
| 117 |
}; |
| 118 |
|
| 119 |
union ol_segval { |
| 120 |
struct ol_sect *sect; /* Section structure */ |
| 121 |
struct ol_sym *sym; /* External symbol structure */ |
| 122 |
}; |
| 123 |
|
| 124 |
struct ol_seg { |
| 125 |
union ol_segval s; |
| 126 |
enum ol_seg_type t; |
| 127 |
|
| 128 |
/* |
| 129 |
* For a section: subsection index |
| 130 |
* For a metasymbol: virtual segment index |
| 131 |
* For an absolute symbol: absolute value |
| 132 |
*/ |
| 133 |
uint32_t index; |
| 134 |
}; |
| 135 |
|
| 136 |
/* seg:offs representing the full location value and type */ |
| 137 |
struct ol_loc { |
| 138 |
int64_t offs; |
| 139 |
struct ol_seg seg; |
| 140 |
}; |
| 141 |
|
| 142 |
/* Common symbol structure */ |
| 143 |
struct ol_sym { |
| 144 |
uint32_t flags; /* Section/symbol flags */ |
| 145 |
uint32_t size; /* Size value (for backend) */ |
| 146 |
struct ol_sym *next; /* Next symbol in declared order */ |
| 147 |
const char *name; /* Symbol name */ |
| 148 |
struct ol_symlist syml; /* Section-local symbol list */ |
| 149 |
struct ol_symlist symg; /* Section-local global symbol list */ |
| 150 |
struct ol_loc p; /* Symbol position ("where") */ |
| 151 |
struct ol_loc v; /* Symbol value ("what") */ |
| 152 |
}; |
| 153 |
|
| 154 |
/* |
| 155 |
* Operations |
| 156 |
*/ |
| 157 |
void ol_init(void); |
| 158 |
void ol_cleanup(void); |
| 159 |
|
| 160 |
/* Convert offs:seg to a location structure */ |
| 161 |
extern void |
| 162 |
ol_mkloc(struct ol_loc *loc, int64_t offs, int32_t seg); |
| 163 |
|
| 164 |
/* Get the section or external symbol from a struct ol_seg */ |
| 165 |
static inline O_Section *seg_sect(struct ol_seg *seg) |
| 166 |
{ |
| 167 |
return (O_Section *)seg->s.sect; |
| 168 |
} |
| 169 |
static inline O_Symbol *seg_xsym(struct ol_seg *seg) |
| 170 |
{ |
| 171 |
return (O_Symbol *)seg->s.sym; |
| 172 |
} |
| 173 |
|
| 174 |
/* |
| 175 |
* Return a pointer to the symbol structure if and only if a section is |
| 176 |
* really a symbol of some kind (extern, common...) |
| 177 |
*/ |
| 178 |
static inline struct ol_sym *_seg_extsym(struct ol_sect *sect) |
| 179 |
{ |
| 180 |
return (sect->flags & OF_SYMBOL) ? (struct ol_sym *)sect : NULL; |
| 181 |
} |
| 182 |
static inline O_Symbol *seg_extsym(O_Section *sect) |
| 183 |
{ |
| 184 |
return (O_Symbol *)_seg_extsym((struct ol_sect *)sect); |
| 185 |
} |
| 186 |
|
| 187 |
/* |
| 188 |
* Find a section or create a new section structure if it does not exist |
| 189 |
* and allocate it an index value via seg_alloc(). |
| 190 |
*/ |
| 191 |
extern struct ol_sect * |
| 192 |
_ol_get_sect(const char *name, size_t ssize, size_t rsize); |
| 193 |
static inline O_Section *ol_get_sect(const char *name) |
| 194 |
{ |
| 195 |
return (O_Section *)_ol_get_sect(name, sizeof(O_Section), sizeof(O_Reloc)); |
| 196 |
} |
| 197 |
|
| 198 |
/* Find a section by name without creating one */ |
| 199 |
extern struct ol_sect *_ol_sect_by_name(const char *); |
| 200 |
static inline O_Section *ol_sect_by_name(const char *name) |
| 201 |
{ |
| 202 |
return (O_Section *)_ol_sect_by_name(name); |
| 203 |
} |
| 204 |
|
| 205 |
/* Find a section or external symbol by index; NULL if not valid */ |
| 206 |
extern struct ol_sect *_ol_sect_by_index(int32_t index); |
| 207 |
static inline O_Section *ol_sect_by_index(int32_t index) |
| 208 |
{ |
| 209 |
return (O_Section *)_ol_sect_by_index(index); |
| 210 |
} |
| 211 |
|
| 212 |
/* Global list of sections (not including external symbols) */ |
| 213 |
extern struct ol_sect *_ol_sect_list; |
| 214 |
static inline O_Section *ol_sect_list(void) |
| 215 |
{ |
| 216 |
return (O_Section *)_ol_sect_list; |
| 217 |
} |
| 218 |
|
| 219 |
/* Count of sections (not including external symbols) */ |
| 220 |
extern uint64_t _ol_nsects; |
| 221 |
static inline uint64_t ol_nsects(void) |
| 222 |
{ |
| 223 |
return _ol_nsects; |
| 224 |
} |
| 225 |
|
| 226 |
/* |
| 227 |
* Start a new subsection for the given section. At the moment, once a |
| 228 |
* subsection has been created, it is not possible to revert to an |
| 229 |
* earlier subsection. ol_sect_by_index() will return the main section |
| 230 |
* structure. Returns the new section index. This is used to prevent |
| 231 |
* the front end from optimizing across subsection boundaries. |
| 232 |
*/ |
| 233 |
extern int32_t _ol_new_subsection(struct ol_sect *sect); |
| 234 |
static inline int32_t ol_new_subsection(O_Section *sect) |
| 235 |
{ |
| 236 |
return _ol_new_subsection((struct ol_sect *)sect); |
| 237 |
} |
| 238 |
|
| 239 |
/* |
| 240 |
* Create a new symbol. If this symbol is OS_OFFS, add it to the relevant |
| 241 |
* section, too. If the symbol already exists, return NULL; this is |
| 242 |
* different from ol_get_section() as a single section may be invoked |
| 243 |
* many times. On the contrary, the front end will prevent a single symbol |
| 244 |
* from being defined more than once. |
| 245 |
* |
| 246 |
* If flags has OF_GLOBAL set, add it to the global symbol hash for the |
| 247 |
* containing section. If flags has OF_IMPSEC set, allocate a segment |
| 248 |
* index for it via seg_alloc() and add it to the section by index list. |
| 249 |
*/ |
| 250 |
extern struct ol_sym *_ol_new_sym(const char *name, const struct ol_loc *v, |
| 251 |
uint32_t flags, size_t size); |
| 252 |
static inline O_Symbol *ol_new_sym(const char *name, const struct ol_loc *v, |
| 253 |
uint32_t flags) |
| 254 |
{ |
| 255 |
return (O_Symbol *)_ol_new_sym(name, v, flags, sizeof(O_Symbol)); |
| 256 |
} |
| 257 |
|
| 258 |
/* Find a symbol by name in the global namespace */ |
| 259 |
extern struct ol_sym *_ol_sym_by_name(const char *name); |
| 260 |
static inline O_Symbol *ol_sym_by_name(const char *name) |
| 261 |
{ |
| 262 |
return (O_Symbol *)_ol_sym_by_name(name); |
| 263 |
} |
| 264 |
|
| 265 |
/* |
| 266 |
* Find a symbol by address in a specific section. If no symbol is defined |
| 267 |
* at that exact address, return the immediately previously defined one. |
| 268 |
* If global is set, then only return global symbols. |
| 269 |
*/ |
| 270 |
extern struct ol_sym *_ol_sym_by_address(struct ol_sect *sect, int64_t addr, |
| 271 |
bool global); |
| 272 |
static inline O_Symbol *ol_sym_by_address(O_Section *sect, int64_t addr, |
| 273 |
bool global) |
| 274 |
{ |
| 275 |
return (O_Symbol *)_ol_sym_by_address((struct ol_sect *)sect, addr, global); |
| 276 |
} |
| 277 |
|
| 278 |
/* Global list of symbols */ |
| 279 |
extern struct ol_sym *_ol_sym_list; |
| 280 |
static inline O_Symbol *ol_sym_list(void) |
| 281 |
{ |
| 282 |
return (O_Symbol *)_ol_sym_list; |
| 283 |
} |
| 284 |
|
| 285 |
/* Global count of symbols */ |
| 286 |
extern uint64_t _ol_nsyms; |
| 287 |
static inline uint64_t ol_nsyms(void) |
| 288 |
{ |
| 289 |
return _ol_nsyms; |
| 290 |
} |
| 291 |
|
| 292 |
#endif /* NASM_OUTLIB_H */ |