symtab.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2009 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  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.
  17. *
  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. * symtab.c Routines to maintain and manipulate a symbol table
  35. *
  36. * These routines donated to the NASM effort by Graeme Defty.
  37. */
  38. #include "rdfutils.h"
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include "symtab.h"
  43. #include "hash.h"
  44. #define SYMTABSIZE 64
  45. #define slotnum(x) (hash((x)) % SYMTABSIZE)
  46. /* ------------------------------------- */
  47. /* Private data types */
  48. typedef struct tagSymtabNode {
  49. struct tagSymtabNode *next;
  50. symtabEnt ent;
  51. } symtabNode;
  52. typedef symtabNode *(symtabTab[SYMTABSIZE]);
  53. typedef symtabTab *symtab;
  54. /* ------------------------------------- */
  55. void *symtabNew(void)
  56. {
  57. symtab mytab;
  58. mytab = (symtabTab *) nasm_calloc(SYMTABSIZE, sizeof(symtabNode *));
  59. if (mytab == NULL) {
  60. fprintf(stderr, "symtab: out of memory\n");
  61. exit(3);
  62. }
  63. return mytab;
  64. }
  65. /* ------------------------------------- */
  66. void symtabDone(void *stab)
  67. {
  68. symtab mytab = (symtab) stab;
  69. int i;
  70. symtabNode *this, *next;
  71. for (i = 0; i < SYMTABSIZE; ++i) {
  72. for (this = (*mytab)[i]; this; this = next) {
  73. next = this->next;
  74. nasm_free(this);
  75. }
  76. }
  77. nasm_free(*mytab);
  78. }
  79. /* ------------------------------------- */
  80. void symtabInsert(void *stab, symtabEnt * ent)
  81. {
  82. symtab mytab = (symtab) stab;
  83. symtabNode *node;
  84. int slot;
  85. node = nasm_malloc(sizeof(symtabNode));
  86. if (node == NULL) {
  87. fprintf(stderr, "symtab: out of memory\n");
  88. exit(3);
  89. }
  90. slot = slotnum(ent->name);
  91. node->ent = *ent;
  92. node->next = (*mytab)[slot];
  93. (*mytab)[slot] = node;
  94. }
  95. /* ------------------------------------- */
  96. symtabEnt *symtabFind(void *stab, const char *name)
  97. {
  98. symtab mytab = (symtab) stab;
  99. int slot = slotnum(name);
  100. symtabNode *node = (*mytab)[slot];
  101. while (node) {
  102. if (!strcmp(node->ent.name, name)) {
  103. return &(node->ent);
  104. }
  105. node = node->next;
  106. }
  107. return NULL;
  108. }
  109. /* ------------------------------------- */
  110. void symtabDump(void *stab, FILE * of)
  111. {
  112. symtab mytab = (symtab) stab;
  113. int i;
  114. char *SegNames[3] = { "code", "data", "bss" };
  115. fprintf(of, "Symbol table is ...\n");
  116. for (i = 0; i < SYMTABSIZE; ++i) {
  117. symtabNode *l = (symtabNode *) (*mytab)[i];
  118. if (l) {
  119. fprintf(of, " ... slot %d ...\n", i);
  120. }
  121. while (l) {
  122. if ((l->ent.segment) == -1) {
  123. fprintf(of, "%-32s Unresolved reference\n", l->ent.name);
  124. } else {
  125. fprintf(of, "%-32s %s:%08"PRIx32" (%"PRId32")\n", l->ent.name,
  126. SegNames[l->ent.segment],
  127. l->ent.offset, l->ent.flags);
  128. }
  129. l = l->next;
  130. }
  131. }
  132. fprintf(of, "........... end of Symbol table.\n");
  133. }