H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
Cyrill Gorcunov | d143f4f | 2010-07-28 10:18:48 +0400 | [diff] [blame] | 2 | * |
H. Peter Anvin | 892c481 | 2018-05-30 14:43:46 -0700 | [diff] [blame] | 3 | * Copyright 1996-2018 The NASM Authors - All Rights Reserved |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 4 | * See the file AUTHORS included with the NASM distribution for |
| 5 | * the specific copyright holders. |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 6 | * |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following |
| 9 | * conditions are met: |
| 10 | * |
| 11 | * * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * * Redistributions in binary form must reproduce the above |
| 14 | * copyright notice, this list of conditions and the following |
| 15 | * disclaimer in the documentation and/or other materials provided |
| 16 | * with the distribution. |
Cyrill Gorcunov | d143f4f | 2010-07-28 10:18:48 +0400 | [diff] [blame] | 17 | * |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | * ----------------------------------------------------------------------- */ |
| 33 | |
| 34 | /* |
| 35 | * labels.c label handling for the Netwide Assembler |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
H. Peter Anvin | fe50195 | 2007-10-02 21:53:51 -0700 | [diff] [blame] | 38 | #include "compiler.h" |
| 39 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 40 | #include <stdio.h> |
| 41 | #include <string.h> |
| 42 | #include <stdlib.h> |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 43 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 44 | #include "nasm.h" |
| 45 | #include "nasmlib.h" |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 46 | #include "error.h" |
H. Peter Anvin | 6244f4b | 2007-09-14 18:03:29 -0700 | [diff] [blame] | 47 | #include "hashtbl.h" |
H. Peter Anvin | c664583 | 2014-11-25 12:07:10 -0800 | [diff] [blame] | 48 | #include "labels.h" |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 49 | |
| 50 | /* |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 51 | * A dot-local label is one that begins with exactly one period. Things |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 52 | * that begin with _two_ periods are NASM-specific things. |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 53 | * |
| 54 | * If TASM compatibility is enabled, a local label can also begin with |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 55 | * @@. |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 56 | */ |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 57 | static bool islocal(const char *l) |
| 58 | { |
| 59 | if (tasm_compatible_mode) { |
| 60 | if (l[0] == '@' && l[1] == '@') |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | return (l[0] == '.' && l[1] != '.'); |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Return true if this falls into NASM's '..' namespace |
| 69 | */ |
| 70 | static bool ismagic(const char *l) |
| 71 | { |
| 72 | return l[0] == '.' && l[1] == '.' && l[2] != '@'; |
| 73 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 74 | |
H. Peter Anvin, Intel | bc77f9c | 2018-06-25 12:39:42 -0700 | [diff] [blame] | 75 | /* |
| 76 | * Return true if we should update the local label base |
| 77 | * as a result of this symbol. We must exclude local labels |
| 78 | * as well as any kind of special labels, including ..@ ones. |
| 79 | */ |
| 80 | static bool set_prevlabel(const char *l) |
| 81 | { |
| 82 | if (tasm_compatible_mode) { |
| 83 | if (l[0] == '@' && l[1] == '@') |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | return l[0] != '.'; |
| 88 | } |
| 89 | |
Cyrill Gorcunov | d143f4f | 2010-07-28 10:18:48 +0400 | [diff] [blame] | 90 | #define LABEL_BLOCK 128 /* no. of labels/block */ |
| 91 | #define LBLK_SIZE (LABEL_BLOCK * sizeof(union label)) |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 92 | |
Cyrill Gorcunov | d143f4f | 2010-07-28 10:18:48 +0400 | [diff] [blame] | 93 | #define END_LIST -3 /* don't clash with NO_SEG! */ |
| 94 | #define END_BLOCK -2 |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 95 | |
Cyrill Gorcunov | d143f4f | 2010-07-28 10:18:48 +0400 | [diff] [blame] | 96 | #define PERMTS_SIZE 16384 /* size of text blocks */ |
H. Peter Anvin | 565be91 | 2009-07-05 22:15:57 -0700 | [diff] [blame] | 97 | #if (PERMTS_SIZE < IDLEN_MAX) |
Cyrill Gorcunov | d143f4f | 2010-07-28 10:18:48 +0400 | [diff] [blame] | 98 | #error "IPERMTS_SIZE must be greater than or equal to IDLEN_MAX" |
Ed Beroset | c06f6df | 2003-09-08 00:30:40 +0000 | [diff] [blame] | 99 | #endif |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 100 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 101 | /* string values for enum label_type */ |
| 102 | static const char * const types[] = |
| 103 | {"local", "global", "static", "extern", "common", "special", |
| 104 | "output format special"}; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 105 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 106 | union label { /* actual label structures */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 107 | struct { |
Charles Crayne | 4e8563d | 2007-11-05 17:19:32 -0800 | [diff] [blame] | 108 | int32_t segment; |
H. Peter Anvin | 29695c8 | 2018-06-14 17:04:32 -0700 | [diff] [blame] | 109 | int32_t subsection; /* Available for ofmt->herelabel() */ |
Cyrill Gorcunov | d143f4f | 2010-07-28 10:18:48 +0400 | [diff] [blame] | 110 | int64_t offset; |
H. Peter Anvin | 7348248 | 2018-06-11 14:54:14 -0700 | [diff] [blame] | 111 | int64_t size; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 112 | char *label, *mangled, *special; |
| 113 | enum label_type type, mangled_type; |
| 114 | bool defined; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 115 | } defn; |
| 116 | struct { |
Charles Crayne | 4e8563d | 2007-11-05 17:19:32 -0800 | [diff] [blame] | 117 | int32_t movingon; |
| 118 | int64_t dummy; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 119 | union label *next; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 120 | } admin; |
| 121 | }; |
| 122 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 123 | struct permts { /* permanent text storage */ |
| 124 | struct permts *next; /* for the linked list */ |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 125 | unsigned int size, usage; /* size and used space in ... */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 126 | char data[PERMTS_SIZE]; /* ... the data block itself */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 127 | }; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 128 | #define PERMTS_HEADER offsetof(struct permts, data) |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 129 | |
H. Peter Anvin | 9c595b6 | 2017-03-07 19:44:21 -0800 | [diff] [blame] | 130 | uint64_t global_offset_changed; /* counter for global offset changes */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 131 | |
Cyrill Gorcunov | d143f4f | 2010-07-28 10:18:48 +0400 | [diff] [blame] | 132 | static struct hash_table ltab; /* labels hash table */ |
| 133 | static union label *ldata; /* all label data blocks */ |
| 134 | static union label *lfree; /* labels free block */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 135 | static struct permts *perm_head; /* start of perm. text storage */ |
| 136 | static struct permts *perm_tail; /* end of perm. text storage */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 137 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 138 | static void init_block(union label *blk); |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 139 | static char *perm_alloc(size_t len); |
H. Peter Anvin | d2fb7a6 | 2007-09-16 17:53:17 -0700 | [diff] [blame] | 140 | static char *perm_copy(const char *string); |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 141 | static char *perm_copy3(const char *s1, const char *s2, const char *s3); |
| 142 | static const char *mangle_label_name(union label *lptr); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 143 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 144 | static const char *prevlabel; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 145 | |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 146 | static bool initialized = false; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 147 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 148 | /* |
H. Peter Anvin | fc0ff22 | 2016-03-07 21:46:04 -0800 | [diff] [blame] | 149 | * Emit a symdef to the output and the debug format backends. |
| 150 | */ |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 151 | static void out_symdef(union label *lptr) |
H. Peter Anvin | fc0ff22 | 2016-03-07 21:46:04 -0800 | [diff] [blame] | 152 | { |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 153 | int backend_type; |
H. Peter Anvin | 7348248 | 2018-06-11 14:54:14 -0700 | [diff] [blame] | 154 | int64_t backend_offset; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 155 | |
| 156 | /* Backend-defined special segments are passed to symdef immediately */ |
| 157 | if (pass0 == 2) { |
| 158 | /* Emit special fixups for globals and commons */ |
| 159 | switch (lptr->defn.type) { |
| 160 | case LBL_GLOBAL: |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 161 | case LBL_EXTERN: |
H. Peter Anvin | 7348248 | 2018-06-11 14:54:14 -0700 | [diff] [blame] | 162 | case LBL_COMMON: |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 163 | if (lptr->defn.special) |
H. Peter Anvin | a7c8e39 | 2018-06-18 14:17:26 -0700 | [diff] [blame] | 164 | ofmt->symdef(lptr->defn.mangled, 0, 0, 3, lptr->defn.special); |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 165 | break; |
| 166 | default: |
| 167 | break; |
| 168 | } |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | if (pass0 != 1 && lptr->defn.type != LBL_BACKEND) |
| 173 | return; |
| 174 | |
| 175 | /* Clean up this hack... */ |
| 176 | switch(lptr->defn.type) { |
| 177 | case LBL_GLOBAL: |
| 178 | backend_type = 1; |
H. Peter Anvin | 7348248 | 2018-06-11 14:54:14 -0700 | [diff] [blame] | 179 | backend_offset = lptr->defn.offset; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 180 | break; |
| 181 | case LBL_COMMON: |
| 182 | backend_type = 2; |
H. Peter Anvin | 7348248 | 2018-06-11 14:54:14 -0700 | [diff] [blame] | 183 | backend_offset = lptr->defn.size; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 184 | break; |
| 185 | default: |
| 186 | backend_type = 0; |
H. Peter Anvin | 7348248 | 2018-06-11 14:54:14 -0700 | [diff] [blame] | 187 | backend_offset = lptr->defn.offset; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 188 | break; |
| 189 | } |
| 190 | |
| 191 | /* Might be necessary for a backend symbol */ |
| 192 | mangle_label_name(lptr); |
| 193 | |
| 194 | ofmt->symdef(lptr->defn.mangled, lptr->defn.segment, |
H. Peter Anvin | 7348248 | 2018-06-11 14:54:14 -0700 | [diff] [blame] | 195 | backend_offset, backend_type, |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 196 | lptr->defn.special); |
H. Peter Anvin | fc0ff22 | 2016-03-07 21:46:04 -0800 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * NASM special symbols are not passed to the debug format; none |
| 200 | * of the current backends want to see them. |
| 201 | */ |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 202 | if (lptr->defn.type == LBL_SPECIAL || lptr->defn.type == LBL_BACKEND) |
| 203 | return; |
| 204 | |
| 205 | dfmt->debug_deflabel(lptr->defn.mangled, lptr->defn.segment, |
| 206 | lptr->defn.offset, backend_type, |
| 207 | lptr->defn.special); |
H. Peter Anvin | fc0ff22 | 2016-03-07 21:46:04 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 211 | * Internal routine: finds the `union label' corresponding to the |
| 212 | * given label name. Creates a new one, if it isn't found, and if |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 213 | * `create' is true. |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 214 | */ |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 215 | static union label *find_label(const char *label, bool create, bool *created) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 216 | { |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 217 | union label *lptr, **lpp; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 218 | char *label_str = NULL; |
H. Peter Anvin | 6244f4b | 2007-09-14 18:03:29 -0700 | [diff] [blame] | 219 | struct hash_insert ip; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 220 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 221 | if (islocal(label)) |
| 222 | label = label_str = nasm_strcat(prevlabel, label); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 223 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 224 | lpp = (union label **) hash_find(<ab, label, &ip); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 225 | lptr = lpp ? *lpp : NULL; |
H. Peter Anvin | 6244f4b | 2007-09-14 18:03:29 -0700 | [diff] [blame] | 226 | |
Cyrill Gorcunov | 5dfcab6 | 2016-07-18 11:49:47 +0300 | [diff] [blame] | 227 | if (lptr || !create) { |
| 228 | if (created) |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 229 | *created = false; |
Cyrill Gorcunov | d143f4f | 2010-07-28 10:18:48 +0400 | [diff] [blame] | 230 | return lptr; |
Cyrill Gorcunov | 5dfcab6 | 2016-07-18 11:49:47 +0300 | [diff] [blame] | 231 | } |
H. Peter Anvin | 6244f4b | 2007-09-14 18:03:29 -0700 | [diff] [blame] | 232 | |
| 233 | /* Create a new label... */ |
| 234 | if (lfree->admin.movingon == END_BLOCK) { |
Cyrill Gorcunov | d143f4f | 2010-07-28 10:18:48 +0400 | [diff] [blame] | 235 | /* |
| 236 | * must allocate a new block |
| 237 | */ |
H. Peter Anvin | 3e55548 | 2017-04-23 17:02:46 -0700 | [diff] [blame] | 238 | lfree->admin.next = nasm_malloc(LBLK_SIZE); |
Cyrill Gorcunov | d143f4f | 2010-07-28 10:18:48 +0400 | [diff] [blame] | 239 | lfree = lfree->admin.next; |
| 240 | init_block(lfree); |
H. Peter Anvin | 6244f4b | 2007-09-14 18:03:29 -0700 | [diff] [blame] | 241 | } |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 242 | |
Cyrill Gorcunov | 5dfcab6 | 2016-07-18 11:49:47 +0300 | [diff] [blame] | 243 | if (created) |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 244 | *created = true; |
Cyrill Gorcunov | 5dfcab6 | 2016-07-18 11:49:47 +0300 | [diff] [blame] | 245 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 246 | nasm_zero(*lfree); |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 247 | lfree->defn.label = perm_copy(label); |
H. Peter Anvin | 29695c8 | 2018-06-14 17:04:32 -0700 | [diff] [blame] | 248 | lfree->defn.subsection = NO_SEG; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 249 | if (label_str) |
| 250 | nasm_free(label_str); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 251 | |
H. Peter Anvin | 6244f4b | 2007-09-14 18:03:29 -0700 | [diff] [blame] | 252 | hash_add(&ip, lfree->defn.label, lfree); |
| 253 | return lfree++; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 254 | } |
| 255 | |
H. Peter Anvin | 785ffb9 | 2017-03-14 18:41:25 -0700 | [diff] [blame] | 256 | bool lookup_label(const char *label, int32_t *segment, int64_t *offset) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 257 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 258 | union label *lptr; |
| 259 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 260 | if (!initialized) |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 261 | return false; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 262 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 263 | lptr = find_label(label, false, NULL); |
| 264 | if (lptr && lptr->defn.defined) { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 265 | *segment = lptr->defn.segment; |
| 266 | *offset = lptr->defn.offset; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 267 | return true; |
Cyrill Gorcunov | d7a64b7 | 2010-04-20 15:26:08 +0400 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | return false; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 271 | } |
| 272 | |
H. Peter Anvin | 785ffb9 | 2017-03-14 18:41:25 -0700 | [diff] [blame] | 273 | bool is_extern(const char *label) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 274 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 275 | union label *lptr; |
| 276 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 277 | if (!initialized) |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 278 | return false; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 279 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 280 | lptr = find_label(label, false, NULL); |
| 281 | return lptr && lptr->defn.type == LBL_EXTERN; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 282 | } |
| 283 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 284 | static const char *mangle_strings[] = {"", "", "", ""}; |
| 285 | static bool mangle_string_set[ARRAY_SIZE(mangle_strings)]; |
| 286 | |
| 287 | /* |
| 288 | * Set a prefix or suffix |
| 289 | */ |
| 290 | void set_label_mangle(enum mangle_index which, const char *what) |
| 291 | { |
| 292 | if (mangle_string_set[which]) |
| 293 | return; /* Once set, do not change */ |
| 294 | |
| 295 | mangle_strings[which] = perm_copy(what); |
| 296 | mangle_string_set[which] = true; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Format a label name with appropriate prefixes and suffixes |
| 301 | */ |
| 302 | static const char *mangle_label_name(union label *lptr) |
| 303 | { |
| 304 | const char *prefix; |
| 305 | const char *suffix; |
| 306 | |
| 307 | if (likely(lptr->defn.mangled && |
| 308 | lptr->defn.mangled_type == lptr->defn.type)) |
| 309 | return lptr->defn.mangled; /* Already mangled */ |
| 310 | |
| 311 | switch (lptr->defn.type) { |
| 312 | case LBL_GLOBAL: |
| 313 | case LBL_STATIC: |
| 314 | case LBL_EXTERN: |
| 315 | prefix = mangle_strings[LM_GPREFIX]; |
| 316 | suffix = mangle_strings[LM_GSUFFIX]; |
| 317 | break; |
| 318 | case LBL_BACKEND: |
| 319 | case LBL_SPECIAL: |
| 320 | prefix = suffix = ""; |
| 321 | break; |
| 322 | default: |
| 323 | prefix = mangle_strings[LM_LPREFIX]; |
| 324 | suffix = mangle_strings[LM_LSUFFIX]; |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | lptr->defn.mangled_type = lptr->defn.type; |
| 329 | |
| 330 | if (!(*prefix) && !(*suffix)) |
| 331 | lptr->defn.mangled = lptr->defn.label; |
| 332 | else |
| 333 | lptr->defn.mangled = perm_copy3(prefix, lptr->defn.label, suffix); |
| 334 | |
| 335 | return lptr->defn.mangled; |
| 336 | } |
| 337 | |
| 338 | static void |
H. Peter Anvin | 29695c8 | 2018-06-14 17:04:32 -0700 | [diff] [blame] | 339 | handle_herelabel(union label *lptr, int32_t *segment, int64_t *offset) |
H. Peter Anvin | 892c481 | 2018-05-30 14:43:46 -0700 | [diff] [blame] | 340 | { |
| 341 | int32_t oldseg; |
| 342 | |
| 343 | if (likely(!ofmt->herelabel)) |
| 344 | return; |
| 345 | |
| 346 | if (unlikely(location.segment == NO_SEG)) |
| 347 | return; |
| 348 | |
| 349 | oldseg = *segment; |
| 350 | |
| 351 | if (oldseg == location.segment && *offset == location.offset) { |
| 352 | /* This label is defined at this location */ |
| 353 | int32_t newseg; |
H. Peter Anvin (Intel) | d644119 | 2018-06-27 20:20:21 -0700 | [diff] [blame] | 354 | bool copyoffset = false; |
H. Peter Anvin | 892c481 | 2018-05-30 14:43:46 -0700 | [diff] [blame] | 355 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 356 | nasm_assert(lptr->defn.mangled); |
H. Peter Anvin | 29695c8 | 2018-06-14 17:04:32 -0700 | [diff] [blame] | 357 | newseg = ofmt->herelabel(lptr->defn.mangled, lptr->defn.type, |
H. Peter Anvin (Intel) | d644119 | 2018-06-27 20:20:21 -0700 | [diff] [blame] | 358 | oldseg, &lptr->defn.subsection, ©offset); |
H. Peter Anvin | 892c481 | 2018-05-30 14:43:46 -0700 | [diff] [blame] | 359 | if (likely(newseg == oldseg)) |
| 360 | return; |
| 361 | |
| 362 | *segment = newseg; |
H. Peter Anvin (Intel) | d644119 | 2018-06-27 20:20:21 -0700 | [diff] [blame] | 363 | if (copyoffset) { |
| 364 | /* Maintain the offset from the old to the new segment */ |
| 365 | switch_segment(newseg); |
| 366 | location.offset = *offset; |
| 367 | } else { |
| 368 | /* Keep a separate offset for the new segment */ |
| 369 | *offset = switch_segment(newseg); |
| 370 | } |
H. Peter Anvin | 892c481 | 2018-05-30 14:43:46 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 374 | static bool declare_label_lptr(union label *lptr, |
| 375 | enum label_type type, const char *special) |
| 376 | { |
H. Peter Anvin | 3cb9068 | 2018-06-01 21:05:45 -0700 | [diff] [blame] | 377 | if (special && !special[0]) |
| 378 | special = NULL; |
| 379 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 380 | if (lptr->defn.type == type || |
| 381 | (pass0 == 0 && lptr->defn.type == LBL_LOCAL)) { |
| 382 | lptr->defn.type = type; |
| 383 | if (special) { |
| 384 | if (!lptr->defn.special) |
| 385 | lptr->defn.special = perm_copy(special); |
| 386 | else if (nasm_stricmp(lptr->defn.special, special)) |
| 387 | nasm_error(ERR_NONFATAL, |
| 388 | "symbol `%s' has inconsistent attributes `%s' and `%s'", |
| 389 | lptr->defn.label, lptr->defn.special, special); |
| 390 | } |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | /* EXTERN can be replaced with GLOBAL or COMMON */ |
| 395 | if (lptr->defn.type == LBL_EXTERN && |
| 396 | (type == LBL_GLOBAL || type == LBL_COMMON)) { |
| 397 | lptr->defn.type = type; |
| 398 | /* Override special unconditionally */ |
| 399 | if (special) |
| 400 | lptr->defn.special = perm_copy(special); |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | /* GLOBAL or COMMON ignore subsequent EXTERN */ |
| 405 | if ((lptr->defn.type == LBL_GLOBAL || lptr->defn.type == LBL_COMMON) && |
| 406 | type == LBL_EXTERN) { |
| 407 | if (!lptr->defn.special) |
| 408 | lptr->defn.special = perm_copy(special); |
H. Peter Anvin, Intel | 4fb2acc | 2018-06-25 13:11:01 -0700 | [diff] [blame] | 409 | return false; /* Don't call define_label() after this! */ |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | nasm_error(ERR_NONFATAL, "symbol `%s' declared both as %s and %s", |
| 413 | lptr->defn.label, types[lptr->defn.type], types[type]); |
| 414 | |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | bool declare_label(const char *label, enum label_type type, const char *special) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 419 | { |
Cyrill Gorcunov | a8e3d6a | 2018-06-30 20:02:24 +0300 | [diff] [blame^] | 420 | union label *lptr = find_label(label, true, NULL); |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 421 | return declare_label_lptr(lptr, type, special); |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * The "normal" argument decides if we should update the local segment |
| 426 | * base name or not. |
| 427 | */ |
| 428 | void define_label(const char *label, int32_t segment, |
| 429 | int64_t offset, bool normal) |
| 430 | { |
| 431 | union label *lptr; |
| 432 | bool created, changed; |
H. Peter Anvin | 7348248 | 2018-06-11 14:54:14 -0700 | [diff] [blame] | 433 | int64_t size; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 434 | |
| 435 | /* |
| 436 | * Phase errors here can be one of two types: a new label appears, |
| 437 | * or the offset changes. Increment global_offset_changed when that |
| 438 | * happens, to tell the assembler core to make another pass. |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 439 | */ |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 440 | lptr = find_label(label, true, &created); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 441 | |
H. Peter Anvin, Intel | c5e45f6 | 2018-06-25 13:16:53 -0700 | [diff] [blame] | 442 | if (segment) { |
| 443 | /* We are actually defining this label */ |
| 444 | if (lptr->defn.type == LBL_EXTERN) /* auto-promote EXTERN to GLOBAL */ |
| 445 | lptr->defn.type = LBL_GLOBAL; |
| 446 | } else { |
| 447 | /* It's a pseudo-segment (extern, common) */ |
H. Peter Anvin | 46c839a | 2018-06-14 20:00:07 -0700 | [diff] [blame] | 448 | segment = lptr->defn.segment ? lptr->defn.segment : seg_alloc(); |
H. Peter Anvin, Intel | c5e45f6 | 2018-06-25 13:16:53 -0700 | [diff] [blame] | 449 | } |
H. Peter Anvin | af5f918 | 2018-06-14 19:53:45 -0700 | [diff] [blame] | 450 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 451 | if (lptr->defn.defined || lptr->defn.type == LBL_BACKEND) { |
| 452 | /* We have seen this on at least one previous pass */ |
| 453 | mangle_label_name(lptr); |
| 454 | handle_herelabel(lptr, &segment, &offset); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 455 | } |
| 456 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 457 | if (ismagic(label) && lptr->defn.type == LBL_LOCAL) |
| 458 | lptr->defn.type = LBL_SPECIAL; |
H. Peter Anvin | 7348248 | 2018-06-11 14:54:14 -0700 | [diff] [blame] | 459 | |
H. Peter Anvin, Intel | bc77f9c | 2018-06-25 12:39:42 -0700 | [diff] [blame] | 460 | if (set_prevlabel(label) && normal) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 461 | prevlabel = lptr->defn.label; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 462 | |
H. Peter Anvin | 7348248 | 2018-06-11 14:54:14 -0700 | [diff] [blame] | 463 | if (lptr->defn.type == LBL_COMMON) { |
| 464 | size = offset; |
| 465 | offset = 0; |
| 466 | } else { |
| 467 | size = 0; /* This is a hack... */ |
| 468 | } |
| 469 | |
H. Peter Anvin, Intel | bc77f9c | 2018-06-25 12:39:42 -0700 | [diff] [blame] | 470 | changed = created || !lptr->defn.defined || |
| 471 | lptr->defn.segment != segment || |
H. Peter Anvin | 7348248 | 2018-06-11 14:54:14 -0700 | [diff] [blame] | 472 | lptr->defn.offset != offset || lptr->defn.size != size; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 473 | global_offset_changed += changed; |
| 474 | |
| 475 | /* |
| 476 | * This probably should be ERR_NONFATAL, but not quite yet. As a |
| 477 | * special case, LBL_SPECIAL symbols are allowed to be changed |
| 478 | * even during the last pass. |
| 479 | */ |
H. Peter Anvin, Intel | bc77f9c | 2018-06-25 12:39:42 -0700 | [diff] [blame] | 480 | if (changed && pass0 > 1 && lptr->defn.type != LBL_SPECIAL) { |
| 481 | nasm_error(ERR_WARNING, "label `%s' %s during code generation", |
H. Peter Anvin, Intel | 2139874 | 2018-06-25 13:03:39 -0700 | [diff] [blame] | 482 | lptr->defn.label, |
| 483 | created ? "defined" : "changed"); |
H. Peter Anvin, Intel | bc77f9c | 2018-06-25 12:39:42 -0700 | [diff] [blame] | 484 | } |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 485 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 486 | lptr->defn.segment = segment; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 487 | lptr->defn.offset = offset; |
H. Peter Anvin | 7348248 | 2018-06-11 14:54:14 -0700 | [diff] [blame] | 488 | lptr->defn.size = size; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 489 | lptr->defn.defined = true; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 490 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 491 | out_symdef(lptr); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 492 | } |
| 493 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 494 | /* |
| 495 | * Define a special backend label |
| 496 | */ |
| 497 | void backend_label(const char *label, int32_t segment, int64_t offset) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 498 | { |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 499 | if (!declare_label(label, LBL_BACKEND, NULL)) |
Cyrill Gorcunov | d143f4f | 2010-07-28 10:18:48 +0400 | [diff] [blame] | 500 | return; |
H. Peter Anvin | aeb0e0e | 2009-06-27 16:30:00 -0700 | [diff] [blame] | 501 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 502 | define_label(label, segment, offset, false); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 503 | } |
| 504 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 505 | int init_labels(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 506 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 507 | hash_init(<ab, HASH_LARGE); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 508 | |
H. Peter Anvin | 3e55548 | 2017-04-23 17:02:46 -0700 | [diff] [blame] | 509 | ldata = lfree = nasm_malloc(LBLK_SIZE); |
H. Peter Anvin | 6244f4b | 2007-09-14 18:03:29 -0700 | [diff] [blame] | 510 | init_block(lfree); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 511 | |
Cyrill Gorcunov | d143f4f | 2010-07-28 10:18:48 +0400 | [diff] [blame] | 512 | perm_head = perm_tail = |
H. Peter Anvin | 3e55548 | 2017-04-23 17:02:46 -0700 | [diff] [blame] | 513 | nasm_malloc(sizeof(struct permts)); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 514 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 515 | perm_head->next = NULL; |
| 516 | perm_head->size = PERMTS_SIZE; |
| 517 | perm_head->usage = 0; |
| 518 | |
| 519 | prevlabel = ""; |
| 520 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 521 | initialized = true; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 522 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 523 | return 0; |
| 524 | } |
| 525 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 526 | void cleanup_labels(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 527 | { |
H. Peter Anvin | 6244f4b | 2007-09-14 18:03:29 -0700 | [diff] [blame] | 528 | union label *lptr, *lhold; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 529 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 530 | initialized = false; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 531 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 532 | hash_free(<ab); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 533 | |
H. Peter Anvin | 6244f4b | 2007-09-14 18:03:29 -0700 | [diff] [blame] | 534 | lptr = lhold = ldata; |
| 535 | while (lptr) { |
Cyrill Gorcunov | d143f4f | 2010-07-28 10:18:48 +0400 | [diff] [blame] | 536 | lptr = &lptr[LABEL_BLOCK-1]; |
| 537 | lptr = lptr->admin.next; |
| 538 | nasm_free(lhold); |
| 539 | lhold = lptr; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | while (perm_head) { |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 543 | perm_tail = perm_head; |
| 544 | perm_head = perm_head->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 545 | nasm_free(perm_tail); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 549 | static void init_block(union label *blk) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 550 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 551 | int j; |
| 552 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 553 | for (j = 0; j < LABEL_BLOCK - 1; j++) |
| 554 | blk[j].admin.movingon = END_LIST; |
| 555 | blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK; |
| 556 | blk[LABEL_BLOCK - 1].admin.next = NULL; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 557 | } |
| 558 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 559 | static char * safe_alloc perm_alloc(size_t len) |
| 560 | { |
H. Peter Anvin | 3cb9068 | 2018-06-01 21:05:45 -0700 | [diff] [blame] | 561 | char *p; |
H. Peter Anvin | 7348248 | 2018-06-11 14:54:14 -0700 | [diff] [blame] | 562 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 563 | if (perm_tail->size - perm_tail->usage < len) { |
| 564 | size_t alloc_len = (len > PERMTS_SIZE) ? len : PERMTS_SIZE; |
| 565 | perm_tail->next = nasm_malloc(PERMTS_HEADER + alloc_len); |
| 566 | perm_tail = perm_tail->next; |
| 567 | perm_tail->next = NULL; |
| 568 | perm_tail->size = alloc_len; |
| 569 | perm_tail->usage = 0; |
| 570 | } |
H. Peter Anvin | 3cb9068 | 2018-06-01 21:05:45 -0700 | [diff] [blame] | 571 | p = perm_tail->data + perm_tail->usage; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 572 | perm_tail->usage += len; |
H. Peter Anvin | 3cb9068 | 2018-06-01 21:05:45 -0700 | [diff] [blame] | 573 | return p; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 574 | } |
| 575 | |
H. Peter Anvin | d2fb7a6 | 2007-09-16 17:53:17 -0700 | [diff] [blame] | 576 | static char *perm_copy(const char *string) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 577 | { |
H. Peter Anvin | d2fb7a6 | 2007-09-16 17:53:17 -0700 | [diff] [blame] | 578 | char *p; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 579 | size_t len; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 580 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 581 | if (!string) |
| 582 | return NULL; |
H. Peter Anvin | 565be91 | 2009-07-05 22:15:57 -0700 | [diff] [blame] | 583 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 584 | len = strlen(string)+1; /* Include final NUL */ |
| 585 | |
| 586 | p = perm_alloc(len); |
H. Peter Anvin | d2fb7a6 | 2007-09-16 17:53:17 -0700 | [diff] [blame] | 587 | memcpy(p, string, len); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 588 | |
| 589 | return p; |
| 590 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 591 | |
H. Peter Anvin | 3cb9068 | 2018-06-01 21:05:45 -0700 | [diff] [blame] | 592 | static char * |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 593 | perm_copy3(const char *s1, const char *s2, const char *s3) |
| 594 | { |
| 595 | char *p; |
| 596 | size_t l1 = strlen(s1); |
| 597 | size_t l2 = strlen(s2); |
| 598 | size_t l3 = strlen(s3)+1; /* Include final NUL */ |
H. Peter Anvin | 7348248 | 2018-06-11 14:54:14 -0700 | [diff] [blame] | 599 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 600 | p = perm_alloc(l1+l2+l3); |
| 601 | memcpy(p, s1, l1); |
| 602 | memcpy(p+l1, s2, l2); |
| 603 | memcpy(p+l1+l2, s3, l3); |
| 604 | |
| 605 | return p; |
| 606 | } |
| 607 | |
| 608 | const char *local_scope(const char *label) |
Charles Crayne | d60059e | 2008-03-12 22:39:03 -0700 | [diff] [blame] | 609 | { |
| 610 | return islocal(label) ? prevlabel : ""; |
| 611 | } |
| 612 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 613 | /* |
| 614 | * Notes regarding bug involving redefinition of external segments. |
| 615 | * |
| 616 | * Up to and including v0.97, the following code didn't work. From 0.97 |
| 617 | * developers release 2 onwards, it will generate an error. |
| 618 | * |
| 619 | * EXTERN extlabel |
| 620 | * newlabel EQU extlabel + 1 |
| 621 | * |
| 622 | * The results of allowing this code through are that two import records |
| 623 | * are generated, one for 'extlabel' and one for 'newlabel'. |
| 624 | * |
| 625 | * The reason for this is an inadequacy in the defined interface between |
| 626 | * the label manager and the output formats. The problem lies in how the |
| 627 | * output format driver tells that a label is an external label for which |
| 628 | * a label import record must be produced. Most (all except bin?) produce |
| 629 | * the record if the segment number of the label is not one of the internal |
| 630 | * segments that the output driver is producing. |
| 631 | * |
| 632 | * A simple fix to this would be to make the output formats keep track of |
| 633 | * which symbols they've produced import records for, and make them not |
| 634 | * produce import records for segments that are already defined. |
| 635 | * |
| 636 | * The best way, which is slightly harder but reduces duplication of code |
| 637 | * and should therefore make the entire system smaller and more stable is |
| 638 | * to change the interface between assembler, define_label(), and |
| 639 | * the output module. The changes that are needed are: |
| 640 | * |
| 641 | * The semantics of the 'isextern' flag passed to define_label() need |
| 642 | * examining. This information may or may not tell us what we need to |
| 643 | * know (ie should we be generating an import record at this point for this |
| 644 | * label). If these aren't the semantics, the semantics should be changed |
| 645 | * to this. |
| 646 | * |
| 647 | * The output module interface needs changing, so that the `isextern' flag |
| 648 | * is passed to the module, so that it can be easily tested for. |
| 649 | */ |