Report dwarf as V2 for ELF32
[platform/upstream/nasm.git] / nasmlib.c
index 6f59820..d5cf207 100644 (file)
--- a/nasmlib.c
+++ b/nasmlib.c
@@ -2,7 +2,7 @@
  *
  * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
  * Julian Hall. All rights reserved. The software is
- * redistributable under the licence given in the file "Licence"
+ * redistributable under the license given in the file "LICENSE"
  * distributed in the NASM archive.
  */
 
@@ -96,10 +96,10 @@ void nasm_free(void *q)
 #endif
 {
     if (q) {
-        free(q);
 #ifdef LOGALLOC
         fprintf(logfp, "%s %d free(%p)\n", file, line, q);
 #endif
+        free(q);
     }
 }
 
@@ -316,7 +316,7 @@ int64_t readnum(char *str, bool *error)
 
     if (warn)
         nasm_malloc_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
-                          "numeric constant %s does not fit in 32 bits",
+                          "numeric constant %s does not fit in 64 bits",
                           str);
 
     return result * sign;
@@ -445,7 +445,7 @@ void standard_extension(char *inname, char *outname, char *extension,
 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
 
-#define LAYERSIZ(r) ( (r)->layers==0 ? RAA_BLKSIZE : RAA_LAYERSIZE )
+#define LAYERSHIFT(r) ( (r)->layers==0 ? RAA_BLKSHIFT : RAA_LAYERSHIFT )
 
 static struct RAA *real_raa_init(int layers)
 {
@@ -454,15 +454,13 @@ static struct RAA *real_raa_init(int layers)
 
     if (layers == 0) {
         r = nasm_zalloc(LEAFSIZ);
-        r->stepsize = 1L;
+        r->shift = 0;
     } else {
         r = nasm_malloc(BRANCHSIZ);
         r->layers = layers;
         for (i = 0; i < RAA_LAYERSIZE; i++)
             r->u.b.data[i] = NULL;
-        r->stepsize = RAA_BLKSIZE;
-        while (--layers)
-            r->stepsize *= RAA_LAYERSIZE;
+        r->shift = (RAA_BLKSHIFT-RAA_LAYERSHIFT) + layers*RAA_LAYERSHIFT;
     }
     return r;
 }
@@ -474,25 +472,23 @@ struct RAA *raa_init(void)
 
 void raa_free(struct RAA *r)
 {
-    if (r->layers == 0)
-        nasm_free(r);
-    else {
+    if (r->layers) {
         struct RAA **p;
         for (p = r->u.b.data; p - r->u.b.data < RAA_LAYERSIZE; p++)
             if (*p)
                 raa_free(*p);
     }
+    nasm_free(r);
 }
 
 int64_t raa_read(struct RAA *r, int32_t posn)
 {
-    if (posn >= r->stepsize * LAYERSIZ(r))
+    if ((uint32_t)posn >= (UINT32_C(1) << (r->shift + LAYERSHIFT(r))))
         return 0;               /* Return 0 for undefined entries */
     while (r->layers > 0) {
-        ldiv_t l;
-        l = ldiv(posn, r->stepsize);
-        r = r->u.b.data[l.quot];
-        posn = l.rem;
+       int32_t l = posn >> r->shift;
+       posn &= (UINT32_C(1) << r->shift)-1;
+        r = r->u.b.data[l];
         if (!r)
             return 0;           /* Return 0 for undefined entries */
     }
@@ -506,7 +502,7 @@ struct RAA *raa_write(struct RAA *r, int32_t posn, int64_t value)
     if (posn < 0)
         nasm_malloc_error(ERR_PANIC, "negative position in raa_write");
 
-    while (r->stepsize * LAYERSIZ(r) <= posn) {
+    while ((UINT32_C(1) << (r->shift+LAYERSHIFT(r))) <= (uint32_t)posn) {
         /*
          * Must add a layer.
          */
@@ -517,7 +513,7 @@ struct RAA *raa_write(struct RAA *r, int32_t posn, int64_t value)
         for (i = 0; i < RAA_LAYERSIZE; i++)
             s->u.b.data[i] = NULL;
         s->layers = r->layers + 1;
-        s->stepsize = LAYERSIZ(r) * r->stepsize;
+        s->shift = LAYERSHIFT(r) + r->shift;
         s->u.b.data[0] = r;
         r = s;
     }
@@ -525,14 +521,13 @@ struct RAA *raa_write(struct RAA *r, int32_t posn, int64_t value)
     result = r;
 
     while (r->layers > 0) {
-        ldiv_t l;
         struct RAA **s;
-        l = ldiv(posn, r->stepsize);
-        s = &r->u.b.data[l.quot];
+       int32_t l = posn >> r->shift;
+       posn &= (UINT32_C(1) << r->shift)-1;
+        s = &r->u.b.data[l];
         if (!*s)
             *s = real_raa_init(r->layers - 1);
         r = *s;
-        posn = l.rem;
     }
 
     r->u.l.data[posn] = value;
@@ -657,6 +652,61 @@ void saa_wbytes(struct SAA *s, const void *data, size_t len)
     }
 }
 
+/* write unsigned LEB128 value to SAA */
+void saa_wleb128u(struct SAA *psaa, int value)
+{
+  char temp[64], *ptemp;
+  uint8_t byte;
+  int len;
+
+  ptemp = temp;
+  len = 0;
+  do
+  {
+     byte = value & 127;
+     value >>= 7;
+     if (value != 0) /* more bytes to come */
+        byte |= 0x80;
+     *ptemp = byte;
+     ptemp++;
+     len++;
+  } while (value != 0);
+  saa_wbytes(psaa, temp, len);  
+}
+
+/* write signed LEB128 value to SAA */
+void saa_wleb128s(struct SAA *psaa, int value)
+{
+  char temp[64], *ptemp;
+  uint8_t byte;
+  bool more, negative;
+  int size, len;
+
+  ptemp = temp;
+  more = 1;
+  negative = (value < 0);
+  size = sizeof(int) * 8;
+  len = 0;
+  while(more)
+  {
+    byte = value & 0x7f;
+    value >>= 7;
+    if (negative)
+     /* sign extend */
+     value |= - (1 <<(size - 7));
+    /* sign bit of byte is second high order bit (0x40) */
+    if ((value == 0 && ! (byte & 0x40)) ||
+       ((value == -1) && (byte & 0x40)))
+       more = 0;
+    else
+      byte |= 0x80;
+    *ptemp = byte;
+    ptemp++;
+    len++;
+  } 
+  saa_wbytes(psaa, temp, len);  
+}
+
 void saa_rewind(struct SAA *s)
 {
     s->rblk = s->blk_ptrs;