2009-06-29 08:13:04 +08:00
|
|
|
/* ----------------------------------------------------------------------- *
|
2010-07-28 14:18:48 +08:00
|
|
|
*
|
2018-05-31 05:43:46 +08:00
|
|
|
* Copyright 1996-2018 The NASM Authors - All Rights Reserved
|
2009-06-29 08:13:04 +08:00
|
|
|
* See the file AUTHORS included with the NASM distribution for
|
|
|
|
* the specific copyright holders.
|
2002-05-01 04:51:32 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
2010-07-28 14:18:48 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* labels.c label handling for the Netwide Assembler
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2007-04-12 10:40:54 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
#include "nasm.h"
|
|
|
|
#include "nasmlib.h"
|
2017-03-08 11:23:03 +08:00
|
|
|
#include "error.h"
|
2007-09-15 09:03:29 +08:00
|
|
|
#include "hashtbl.h"
|
2014-11-26 04:07:10 +08:00
|
|
|
#include "labels.h"
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
2018-06-02 09:02:54 +08:00
|
|
|
* A dot-local label is one that begins with exactly one period. Things
|
2002-05-01 04:51:32 +08:00
|
|
|
* that begin with _two_ periods are NASM-specific things.
|
2002-05-01 05:00:33 +08:00
|
|
|
*
|
|
|
|
* If TASM compatibility is enabled, a local label can also begin with
|
2018-06-02 09:02:54 +08:00
|
|
|
* @@.
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
2018-06-02 09:02:54 +08:00
|
|
|
static bool islocal(const char *l)
|
|
|
|
{
|
|
|
|
if (tasm_compatible_mode) {
|
|
|
|
if (l[0] == '@' && l[1] == '@')
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (l[0] == '.' && l[1] != '.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return true if this falls into NASM's '..' namespace
|
|
|
|
*/
|
|
|
|
static bool ismagic(const char *l)
|
|
|
|
{
|
|
|
|
return l[0] == '.' && l[1] == '.' && l[2] != '@';
|
|
|
|
}
|
2010-07-28 14:18:48 +08:00
|
|
|
|
2018-06-26 03:39:42 +08:00
|
|
|
/*
|
|
|
|
* Return true if we should update the local label base
|
|
|
|
* as a result of this symbol. We must exclude local labels
|
|
|
|
* as well as any kind of special labels, including ..@ ones.
|
|
|
|
*/
|
|
|
|
static bool set_prevlabel(const char *l)
|
|
|
|
{
|
|
|
|
if (tasm_compatible_mode) {
|
|
|
|
if (l[0] == '@' && l[1] == '@')
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return l[0] != '.';
|
|
|
|
}
|
|
|
|
|
2010-07-28 14:18:48 +08:00
|
|
|
#define LABEL_BLOCK 128 /* no. of labels/block */
|
|
|
|
#define LBLK_SIZE (LABEL_BLOCK * sizeof(union label))
|
|
|
|
|
|
|
|
#define END_LIST -3 /* don't clash with NO_SEG! */
|
|
|
|
#define END_BLOCK -2
|
|
|
|
|
|
|
|
#define PERMTS_SIZE 16384 /* size of text blocks */
|
2009-07-06 13:15:57 +08:00
|
|
|
#if (PERMTS_SIZE < IDLEN_MAX)
|
2010-07-28 14:18:48 +08:00
|
|
|
#error "IPERMTS_SIZE must be greater than or equal to IDLEN_MAX"
|
2003-09-08 08:30:40 +08:00
|
|
|
#endif
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
/* string values for enum label_type */
|
|
|
|
static const char * const types[] =
|
|
|
|
{"local", "global", "static", "extern", "common", "special",
|
|
|
|
"output format special"};
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
union label { /* actual label structures */
|
2002-05-01 04:51:32 +08:00
|
|
|
struct {
|
2007-11-06 09:19:32 +08:00
|
|
|
int32_t segment;
|
2018-06-15 08:04:32 +08:00
|
|
|
int32_t subsection; /* Available for ofmt->herelabel() */
|
2010-07-28 14:18:48 +08:00
|
|
|
int64_t offset;
|
2018-06-12 05:54:14 +08:00
|
|
|
int64_t size;
|
2018-06-02 09:02:54 +08:00
|
|
|
char *label, *mangled, *special;
|
|
|
|
enum label_type type, mangled_type;
|
|
|
|
bool defined;
|
2002-05-01 04:51:32 +08:00
|
|
|
} defn;
|
|
|
|
struct {
|
2007-11-06 09:19:32 +08:00
|
|
|
int32_t movingon;
|
|
|
|
int64_t dummy;
|
2002-05-01 04:59:21 +08:00
|
|
|
union label *next;
|
2002-05-01 04:51:32 +08:00
|
|
|
} admin;
|
|
|
|
};
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
struct permts { /* permanent text storage */
|
|
|
|
struct permts *next; /* for the linked list */
|
2018-06-02 09:02:54 +08:00
|
|
|
unsigned int size, usage; /* size and used space in ... */
|
2007-04-14 00:47:53 +08:00
|
|
|
char data[PERMTS_SIZE]; /* ... the data block itself */
|
2002-05-01 04:51:32 +08:00
|
|
|
};
|
2018-06-02 09:02:54 +08:00
|
|
|
#define PERMTS_HEADER offsetof(struct permts, data)
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2017-03-08 11:44:21 +08:00
|
|
|
uint64_t global_offset_changed; /* counter for global offset changes */
|
2002-05-01 04:59:21 +08:00
|
|
|
|
2010-07-28 14:18:48 +08:00
|
|
|
static struct hash_table ltab; /* labels hash table */
|
|
|
|
static union label *ldata; /* all label data blocks */
|
|
|
|
static union label *lfree; /* labels free block */
|
2005-01-16 06:15:51 +08:00
|
|
|
static struct permts *perm_head; /* start of perm. text storage */
|
|
|
|
static struct permts *perm_tail; /* end of perm. text storage */
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static void init_block(union label *blk);
|
2018-06-02 09:02:54 +08:00
|
|
|
static char *perm_alloc(size_t len);
|
2007-09-17 08:53:17 +08:00
|
|
|
static char *perm_copy(const char *string);
|
2018-06-02 09:02:54 +08:00
|
|
|
static char *perm_copy3(const char *s1, const char *s2, const char *s3);
|
|
|
|
static const char *mangle_label_name(union label *lptr);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
static const char *prevlabel;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-11 15:05:31 +08:00
|
|
|
static bool initialized = false;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2016-03-08 13:46:04 +08:00
|
|
|
/*
|
|
|
|
* Emit a symdef to the output and the debug format backends.
|
|
|
|
*/
|
2018-06-02 09:02:54 +08:00
|
|
|
static void out_symdef(union label *lptr)
|
2016-03-08 13:46:04 +08:00
|
|
|
{
|
2018-06-02 09:02:54 +08:00
|
|
|
int backend_type;
|
2018-06-12 05:54:14 +08:00
|
|
|
int64_t backend_offset;
|
2018-06-02 09:02:54 +08:00
|
|
|
|
|
|
|
/* Backend-defined special segments are passed to symdef immediately */
|
|
|
|
if (pass0 == 2) {
|
|
|
|
/* Emit special fixups for globals and commons */
|
|
|
|
switch (lptr->defn.type) {
|
|
|
|
case LBL_GLOBAL:
|
|
|
|
case LBL_EXTERN:
|
2018-06-12 05:54:14 +08:00
|
|
|
case LBL_COMMON:
|
2018-06-02 09:02:54 +08:00
|
|
|
if (lptr->defn.special)
|
2018-06-19 05:17:26 +08:00
|
|
|
ofmt->symdef(lptr->defn.mangled, 0, 0, 3, lptr->defn.special);
|
2018-06-02 09:02:54 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pass0 != 1 && lptr->defn.type != LBL_BACKEND)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Clean up this hack... */
|
|
|
|
switch(lptr->defn.type) {
|
|
|
|
case LBL_GLOBAL:
|
2018-07-01 06:59:07 +08:00
|
|
|
case LBL_EXTERN:
|
2018-06-02 09:02:54 +08:00
|
|
|
backend_type = 1;
|
2018-06-12 05:54:14 +08:00
|
|
|
backend_offset = lptr->defn.offset;
|
2018-06-02 09:02:54 +08:00
|
|
|
break;
|
|
|
|
case LBL_COMMON:
|
|
|
|
backend_type = 2;
|
2018-06-12 05:54:14 +08:00
|
|
|
backend_offset = lptr->defn.size;
|
2018-06-02 09:02:54 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
backend_type = 0;
|
2018-06-12 05:54:14 +08:00
|
|
|
backend_offset = lptr->defn.offset;
|
2018-06-02 09:02:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Might be necessary for a backend symbol */
|
|
|
|
mangle_label_name(lptr);
|
|
|
|
|
|
|
|
ofmt->symdef(lptr->defn.mangled, lptr->defn.segment,
|
2018-06-12 05:54:14 +08:00
|
|
|
backend_offset, backend_type,
|
2018-06-02 09:02:54 +08:00
|
|
|
lptr->defn.special);
|
2016-03-08 13:46:04 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NASM special symbols are not passed to the debug format; none
|
|
|
|
* of the current backends want to see them.
|
|
|
|
*/
|
2018-06-02 09:02:54 +08:00
|
|
|
if (lptr->defn.type == LBL_SPECIAL || lptr->defn.type == LBL_BACKEND)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dfmt->debug_deflabel(lptr->defn.mangled, lptr->defn.segment,
|
|
|
|
lptr->defn.offset, backend_type,
|
|
|
|
lptr->defn.special);
|
2016-03-08 13:46:04 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
|
|
|
* Internal routine: finds the `union label' corresponding to the
|
|
|
|
* given label name. Creates a new one, if it isn't found, and if
|
2007-10-11 05:58:45 +08:00
|
|
|
* `create' is true.
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
2018-06-02 09:02:54 +08:00
|
|
|
static union label *find_label(const char *label, bool create, bool *created)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-09-17 08:57:25 +08:00
|
|
|
union label *lptr, **lpp;
|
2018-06-02 09:02:54 +08:00
|
|
|
char *label_str = NULL;
|
2007-09-15 09:03:29 +08:00
|
|
|
struct hash_insert ip;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2018-10-13 22:18:05 +08:00
|
|
|
nasm_assert(label != NULL);
|
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
if (islocal(label))
|
|
|
|
label = label_str = nasm_strcat(prevlabel, label);
|
2002-05-01 04:59:21 +08:00
|
|
|
|
2008-05-29 03:28:58 +08:00
|
|
|
lpp = (union label **) hash_find(<ab, label, &ip);
|
2007-09-17 08:57:25 +08:00
|
|
|
lptr = lpp ? *lpp : NULL;
|
2007-09-15 09:03:29 +08:00
|
|
|
|
2016-07-18 16:49:47 +08:00
|
|
|
if (lptr || !create) {
|
|
|
|
if (created)
|
2018-06-02 09:02:54 +08:00
|
|
|
*created = false;
|
2010-07-28 14:18:48 +08:00
|
|
|
return lptr;
|
2016-07-18 16:49:47 +08:00
|
|
|
}
|
2007-09-15 09:03:29 +08:00
|
|
|
|
|
|
|
/* Create a new label... */
|
|
|
|
if (lfree->admin.movingon == END_BLOCK) {
|
2010-07-28 14:18:48 +08:00
|
|
|
/*
|
|
|
|
* must allocate a new block
|
|
|
|
*/
|
2017-04-24 08:02:46 +08:00
|
|
|
lfree->admin.next = nasm_malloc(LBLK_SIZE);
|
2010-07-28 14:18:48 +08:00
|
|
|
lfree = lfree->admin.next;
|
|
|
|
init_block(lfree);
|
2007-09-15 09:03:29 +08:00
|
|
|
}
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2016-07-18 16:49:47 +08:00
|
|
|
if (created)
|
2018-06-02 09:02:54 +08:00
|
|
|
*created = true;
|
2016-07-18 16:49:47 +08:00
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
nasm_zero(*lfree);
|
|
|
|
lfree->defn.label = perm_copy(label);
|
2018-06-15 08:04:32 +08:00
|
|
|
lfree->defn.subsection = NO_SEG;
|
2018-06-02 09:02:54 +08:00
|
|
|
if (label_str)
|
|
|
|
nasm_free(label_str);
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-09-15 09:03:29 +08:00
|
|
|
hash_add(&ip, lfree->defn.label, lfree);
|
|
|
|
return lfree++;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2017-03-15 09:41:25 +08:00
|
|
|
bool lookup_label(const char *label, int32_t *segment, int64_t *offset)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
union label *lptr;
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
if (!initialized)
|
2007-10-11 15:05:31 +08:00
|
|
|
return false;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
lptr = find_label(label, false, NULL);
|
|
|
|
if (lptr && lptr->defn.defined) {
|
2002-05-01 04:59:21 +08:00
|
|
|
*segment = lptr->defn.segment;
|
|
|
|
*offset = lptr->defn.offset;
|
2007-10-11 15:05:31 +08:00
|
|
|
return true;
|
2010-04-20 19:26:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2017-03-15 09:41:25 +08:00
|
|
|
bool is_extern(const char *label)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
union label *lptr;
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
if (!initialized)
|
2007-10-11 15:05:31 +08:00
|
|
|
return false;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
lptr = find_label(label, false, NULL);
|
|
|
|
return lptr && lptr->defn.type == LBL_EXTERN;
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
static const char *mangle_strings[] = {"", "", "", ""};
|
|
|
|
static bool mangle_string_set[ARRAY_SIZE(mangle_strings)];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set a prefix or suffix
|
|
|
|
*/
|
|
|
|
void set_label_mangle(enum mangle_index which, const char *what)
|
|
|
|
{
|
|
|
|
if (mangle_string_set[which])
|
|
|
|
return; /* Once set, do not change */
|
|
|
|
|
|
|
|
mangle_strings[which] = perm_copy(what);
|
|
|
|
mangle_string_set[which] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Format a label name with appropriate prefixes and suffixes
|
|
|
|
*/
|
|
|
|
static const char *mangle_label_name(union label *lptr)
|
|
|
|
{
|
|
|
|
const char *prefix;
|
|
|
|
const char *suffix;
|
|
|
|
|
|
|
|
if (likely(lptr->defn.mangled &&
|
|
|
|
lptr->defn.mangled_type == lptr->defn.type))
|
|
|
|
return lptr->defn.mangled; /* Already mangled */
|
|
|
|
|
|
|
|
switch (lptr->defn.type) {
|
|
|
|
case LBL_GLOBAL:
|
|
|
|
case LBL_STATIC:
|
|
|
|
case LBL_EXTERN:
|
|
|
|
prefix = mangle_strings[LM_GPREFIX];
|
|
|
|
suffix = mangle_strings[LM_GSUFFIX];
|
|
|
|
break;
|
|
|
|
case LBL_BACKEND:
|
|
|
|
case LBL_SPECIAL:
|
|
|
|
prefix = suffix = "";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
prefix = mangle_strings[LM_LPREFIX];
|
|
|
|
suffix = mangle_strings[LM_LSUFFIX];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
lptr->defn.mangled_type = lptr->defn.type;
|
|
|
|
|
|
|
|
if (!(*prefix) && !(*suffix))
|
|
|
|
lptr->defn.mangled = lptr->defn.label;
|
|
|
|
else
|
|
|
|
lptr->defn.mangled = perm_copy3(prefix, lptr->defn.label, suffix);
|
|
|
|
|
|
|
|
return lptr->defn.mangled;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-06-15 08:04:32 +08:00
|
|
|
handle_herelabel(union label *lptr, int32_t *segment, int64_t *offset)
|
2018-05-31 05:43:46 +08:00
|
|
|
{
|
|
|
|
int32_t oldseg;
|
|
|
|
|
|
|
|
if (likely(!ofmt->herelabel))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (unlikely(location.segment == NO_SEG))
|
|
|
|
return;
|
|
|
|
|
|
|
|
oldseg = *segment;
|
|
|
|
|
|
|
|
if (oldseg == location.segment && *offset == location.offset) {
|
|
|
|
/* This label is defined at this location */
|
|
|
|
int32_t newseg;
|
2018-06-28 11:20:21 +08:00
|
|
|
bool copyoffset = false;
|
2018-05-31 05:43:46 +08:00
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
nasm_assert(lptr->defn.mangled);
|
2018-06-15 08:04:32 +08:00
|
|
|
newseg = ofmt->herelabel(lptr->defn.mangled, lptr->defn.type,
|
2018-06-28 11:20:21 +08:00
|
|
|
oldseg, &lptr->defn.subsection, ©offset);
|
2018-05-31 05:43:46 +08:00
|
|
|
if (likely(newseg == oldseg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
*segment = newseg;
|
2018-06-28 11:20:21 +08:00
|
|
|
if (copyoffset) {
|
|
|
|
/* Maintain the offset from the old to the new segment */
|
|
|
|
switch_segment(newseg);
|
|
|
|
location.offset = *offset;
|
|
|
|
} else {
|
|
|
|
/* Keep a separate offset for the new segment */
|
|
|
|
*offset = switch_segment(newseg);
|
|
|
|
}
|
2018-05-31 05:43:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
static bool declare_label_lptr(union label *lptr,
|
|
|
|
enum label_type type, const char *special)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2018-06-02 12:05:45 +08:00
|
|
|
if (special && !special[0])
|
|
|
|
special = NULL;
|
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
if (lptr->defn.type == type ||
|
|
|
|
(pass0 == 0 && lptr->defn.type == LBL_LOCAL)) {
|
|
|
|
lptr->defn.type = type;
|
|
|
|
if (special) {
|
|
|
|
if (!lptr->defn.special)
|
|
|
|
lptr->defn.special = perm_copy(special);
|
|
|
|
else if (nasm_stricmp(lptr->defn.special, special))
|
|
|
|
nasm_error(ERR_NONFATAL,
|
|
|
|
"symbol `%s' has inconsistent attributes `%s' and `%s'",
|
|
|
|
lptr->defn.label, lptr->defn.special, special);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-07-18 16:49:47 +08:00
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
/* EXTERN can be replaced with GLOBAL or COMMON */
|
|
|
|
if (lptr->defn.type == LBL_EXTERN &&
|
|
|
|
(type == LBL_GLOBAL || type == LBL_COMMON)) {
|
|
|
|
lptr->defn.type = type;
|
|
|
|
/* Override special unconditionally */
|
|
|
|
if (special)
|
|
|
|
lptr->defn.special = perm_copy(special);
|
|
|
|
return true;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2002-05-01 04:59:21 +08:00
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
/* GLOBAL or COMMON ignore subsequent EXTERN */
|
|
|
|
if ((lptr->defn.type == LBL_GLOBAL || lptr->defn.type == LBL_COMMON) &&
|
|
|
|
type == LBL_EXTERN) {
|
|
|
|
if (!lptr->defn.special)
|
|
|
|
lptr->defn.special = perm_copy(special);
|
2018-06-26 04:11:01 +08:00
|
|
|
return false; /* Don't call define_label() after this! */
|
2018-06-02 09:02:54 +08:00
|
|
|
}
|
2009-06-28 07:30:00 +08:00
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
nasm_error(ERR_NONFATAL, "symbol `%s' declared both as %s and %s",
|
|
|
|
lptr->defn.label, types[lptr->defn.type], types[type]);
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
return false;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
bool declare_label(const char *label, enum label_type type, const char *special)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2018-07-01 01:02:24 +08:00
|
|
|
union label *lptr = find_label(label, true, NULL);
|
2018-06-02 09:02:54 +08:00
|
|
|
return declare_label_lptr(lptr, type, special);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
/*
|
|
|
|
* The "normal" argument decides if we should update the local segment
|
|
|
|
* base name or not.
|
|
|
|
*/
|
|
|
|
void define_label(const char *label, int32_t segment,
|
|
|
|
int64_t offset, bool normal)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
union label *lptr;
|
2018-06-02 09:02:54 +08:00
|
|
|
bool created, changed;
|
2018-06-12 05:54:14 +08:00
|
|
|
int64_t size;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
/*
|
|
|
|
* Phase errors here can be one of two types: a new label appears,
|
|
|
|
* or the offset changes. Increment global_offset_changed when that
|
|
|
|
* happens, to tell the assembler core to make another pass.
|
|
|
|
*/
|
|
|
|
lptr = find_label(label, true, &created);
|
|
|
|
|
2018-06-26 04:16:53 +08:00
|
|
|
if (segment) {
|
|
|
|
/* We are actually defining this label */
|
|
|
|
if (lptr->defn.type == LBL_EXTERN) /* auto-promote EXTERN to GLOBAL */
|
|
|
|
lptr->defn.type = LBL_GLOBAL;
|
|
|
|
} else {
|
|
|
|
/* It's a pseudo-segment (extern, common) */
|
2018-06-15 11:00:07 +08:00
|
|
|
segment = lptr->defn.segment ? lptr->defn.segment : seg_alloc();
|
2018-06-26 04:16:53 +08:00
|
|
|
}
|
2018-06-15 10:53:45 +08:00
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
if (lptr->defn.defined || lptr->defn.type == LBL_BACKEND) {
|
|
|
|
/* We have seen this on at least one previous pass */
|
|
|
|
mangle_label_name(lptr);
|
|
|
|
handle_herelabel(lptr, &segment, &offset);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
if (ismagic(label) && lptr->defn.type == LBL_LOCAL)
|
|
|
|
lptr->defn.type = LBL_SPECIAL;
|
2018-06-12 05:54:14 +08:00
|
|
|
|
2018-06-26 03:39:42 +08:00
|
|
|
if (set_prevlabel(label) && normal)
|
2002-05-01 04:59:21 +08:00
|
|
|
prevlabel = lptr->defn.label;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2018-06-12 05:54:14 +08:00
|
|
|
if (lptr->defn.type == LBL_COMMON) {
|
|
|
|
size = offset;
|
|
|
|
offset = 0;
|
|
|
|
} else {
|
|
|
|
size = 0; /* This is a hack... */
|
|
|
|
}
|
|
|
|
|
2018-06-26 03:39:42 +08:00
|
|
|
changed = created || !lptr->defn.defined ||
|
|
|
|
lptr->defn.segment != segment ||
|
2018-06-12 05:54:14 +08:00
|
|
|
lptr->defn.offset != offset || lptr->defn.size != size;
|
2018-06-02 09:02:54 +08:00
|
|
|
global_offset_changed += changed;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
/*
|
|
|
|
* This probably should be ERR_NONFATAL, but not quite yet. As a
|
|
|
|
* special case, LBL_SPECIAL symbols are allowed to be changed
|
|
|
|
* even during the last pass.
|
|
|
|
*/
|
2018-06-26 03:39:42 +08:00
|
|
|
if (changed && pass0 > 1 && lptr->defn.type != LBL_SPECIAL) {
|
|
|
|
nasm_error(ERR_WARNING, "label `%s' %s during code generation",
|
2018-06-26 04:03:39 +08:00
|
|
|
lptr->defn.label,
|
|
|
|
created ? "defined" : "changed");
|
2018-06-26 03:39:42 +08:00
|
|
|
}
|
2018-06-02 09:02:54 +08:00
|
|
|
|
|
|
|
lptr->defn.segment = segment;
|
|
|
|
lptr->defn.offset = offset;
|
2018-06-12 05:54:14 +08:00
|
|
|
lptr->defn.size = size;
|
2018-06-02 09:02:54 +08:00
|
|
|
lptr->defn.defined = true;
|
2009-06-28 07:30:00 +08:00
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
out_symdef(lptr);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
/*
|
|
|
|
* Define a special backend label
|
|
|
|
*/
|
|
|
|
void backend_label(const char *label, int32_t segment, int64_t offset)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2018-06-02 09:02:54 +08:00
|
|
|
if (!declare_label(label, LBL_BACKEND, NULL))
|
2002-05-01 04:59:21 +08:00
|
|
|
return;
|
2018-06-02 09:02:54 +08:00
|
|
|
|
|
|
|
define_label(label, segment, offset, false);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
int init_labels(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2008-05-29 03:28:58 +08:00
|
|
|
hash_init(<ab, HASH_LARGE);
|
2007-09-15 09:03:29 +08:00
|
|
|
|
2017-04-24 08:02:46 +08:00
|
|
|
ldata = lfree = nasm_malloc(LBLK_SIZE);
|
2007-09-15 09:03:29 +08:00
|
|
|
init_block(lfree);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2010-07-28 14:18:48 +08:00
|
|
|
perm_head = perm_tail =
|
2017-04-24 08:02:46 +08:00
|
|
|
nasm_malloc(sizeof(struct permts));
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
perm_head->next = NULL;
|
|
|
|
perm_head->size = PERMTS_SIZE;
|
|
|
|
perm_head->usage = 0;
|
|
|
|
|
|
|
|
prevlabel = "";
|
|
|
|
|
2007-10-11 05:58:45 +08:00
|
|
|
initialized = true;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
void cleanup_labels(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-09-15 09:03:29 +08:00
|
|
|
union label *lptr, *lhold;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-11 05:58:45 +08:00
|
|
|
initialized = false;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2008-05-29 03:28:58 +08:00
|
|
|
hash_free(<ab);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-09-15 09:03:29 +08:00
|
|
|
lptr = lhold = ldata;
|
|
|
|
while (lptr) {
|
2010-07-28 14:18:48 +08:00
|
|
|
lptr = &lptr[LABEL_BLOCK-1];
|
|
|
|
lptr = lptr->admin.next;
|
|
|
|
nasm_free(lhold);
|
|
|
|
lhold = lptr;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (perm_head) {
|
2002-05-01 04:59:21 +08:00
|
|
|
perm_tail = perm_head;
|
|
|
|
perm_head = perm_head->next;
|
2005-01-16 06:15:51 +08:00
|
|
|
nasm_free(perm_tail);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static void init_block(union label *blk)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
int j;
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
for (j = 0; j < LABEL_BLOCK - 1; j++)
|
|
|
|
blk[j].admin.movingon = END_LIST;
|
|
|
|
blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
|
|
|
|
blk[LABEL_BLOCK - 1].admin.next = NULL;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
static char * safe_alloc perm_alloc(size_t len)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2018-06-02 12:05:45 +08:00
|
|
|
char *p;
|
2018-06-12 05:54:14 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
if (perm_tail->size - perm_tail->usage < len) {
|
2018-06-02 09:02:54 +08:00
|
|
|
size_t alloc_len = (len > PERMTS_SIZE) ? len : PERMTS_SIZE;
|
|
|
|
perm_tail->next = nasm_malloc(PERMTS_HEADER + alloc_len);
|
2002-05-01 04:59:21 +08:00
|
|
|
perm_tail = perm_tail->next;
|
|
|
|
perm_tail->next = NULL;
|
2018-06-02 09:02:54 +08:00
|
|
|
perm_tail->size = alloc_len;
|
2002-05-01 04:59:21 +08:00
|
|
|
perm_tail->usage = 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2018-06-02 12:05:45 +08:00
|
|
|
p = perm_tail->data + perm_tail->usage;
|
2007-09-17 08:53:17 +08:00
|
|
|
perm_tail->usage += len;
|
2018-06-02 12:05:45 +08:00
|
|
|
return p;
|
2018-06-02 09:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *perm_copy(const char *string)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (!string)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
len = strlen(string)+1; /* Include final NUL */
|
|
|
|
|
|
|
|
p = perm_alloc(len);
|
|
|
|
memcpy(p, string, len);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2018-06-02 12:05:45 +08:00
|
|
|
static char *
|
2018-06-02 09:02:54 +08:00
|
|
|
perm_copy3(const char *s1, const char *s2, const char *s3)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
size_t l1 = strlen(s1);
|
|
|
|
size_t l2 = strlen(s2);
|
|
|
|
size_t l3 = strlen(s3)+1; /* Include final NUL */
|
2018-06-12 05:54:14 +08:00
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
p = perm_alloc(l1+l2+l3);
|
|
|
|
memcpy(p, s1, l1);
|
|
|
|
memcpy(p+l1, s2, l2);
|
|
|
|
memcpy(p+l1+l2, s3, l3);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
const char *local_scope(const char *label)
|
2008-03-13 13:39:03 +08:00
|
|
|
{
|
|
|
|
return islocal(label) ? prevlabel : "";
|
|
|
|
}
|
|
|
|
|
2002-05-01 04:53:55 +08:00
|
|
|
/*
|
|
|
|
* Notes regarding bug involving redefinition of external segments.
|
|
|
|
*
|
|
|
|
* Up to and including v0.97, the following code didn't work. From 0.97
|
|
|
|
* developers release 2 onwards, it will generate an error.
|
|
|
|
*
|
|
|
|
* EXTERN extlabel
|
|
|
|
* newlabel EQU extlabel + 1
|
|
|
|
*
|
|
|
|
* The results of allowing this code through are that two import records
|
|
|
|
* are generated, one for 'extlabel' and one for 'newlabel'.
|
|
|
|
*
|
|
|
|
* The reason for this is an inadequacy in the defined interface between
|
|
|
|
* the label manager and the output formats. The problem lies in how the
|
|
|
|
* output format driver tells that a label is an external label for which
|
|
|
|
* a label import record must be produced. Most (all except bin?) produce
|
|
|
|
* the record if the segment number of the label is not one of the internal
|
|
|
|
* segments that the output driver is producing.
|
|
|
|
*
|
|
|
|
* A simple fix to this would be to make the output formats keep track of
|
|
|
|
* which symbols they've produced import records for, and make them not
|
|
|
|
* produce import records for segments that are already defined.
|
|
|
|
*
|
|
|
|
* The best way, which is slightly harder but reduces duplication of code
|
|
|
|
* and should therefore make the entire system smaller and more stable is
|
|
|
|
* to change the interface between assembler, define_label(), and
|
|
|
|
* the output module. The changes that are needed are:
|
|
|
|
*
|
|
|
|
* The semantics of the 'isextern' flag passed to define_label() need
|
|
|
|
* examining. This information may or may not tell us what we need to
|
|
|
|
* know (ie should we be generating an import record at this point for this
|
|
|
|
* label). If these aren't the semantics, the semantics should be changed
|
|
|
|
* to this.
|
|
|
|
*
|
|
|
|
* The output module interface needs changing, so that the `isextern' flag
|
|
|
|
* is passed to the module, so that it can be easily tested for.
|
|
|
|
*/
|