reloc-macros.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Generic relocation support for BFD.
  2. Copyright (C) 1998-2015 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  15. /* These macros are used by the various *.h target specific header
  16. files to either generate an enum containing all the known relocations
  17. for that target, or if RELOC_MACROS_GEN_FUNC is defined, a recognition
  18. function is generated instead. (This is used by binutils/readelf.c)
  19. Given a header file like this:
  20. START_RELOC_NUMBERS (foo)
  21. RELOC_NUMBER (R_foo_NONE, 0)
  22. RELOC_NUMBER (R_foo_32, 1)
  23. EMPTY_RELOC (R_foo_good)
  24. FAKE_RELOC (R_foo_illegal, 9)
  25. END_RELOC_NUMBERS (R_foo_count)
  26. Then the following will be produced by default (ie if
  27. RELOC_MACROS_GEN_FUNC is *not* defined).
  28. enum foo
  29. {
  30. R_foo_NONE = 0,
  31. R_foo_32 = 1,
  32. R_foo_good,
  33. R_foo_illegal = 9,
  34. R_foo_count
  35. };
  36. Note: The value of the symbol defined in the END_RELOC_NUMBERS
  37. macro (R_foo_count in the case of the example above) will be
  38. set to the value of the whichever *_RELOC macro precedes it plus
  39. one. Therefore if you intend to use the symbol as a sentinel for
  40. the highest valid macro value you should make sure that the
  41. preceding *_RELOC macro is the highest valid number. ie a
  42. declaration like this:
  43. START_RELOC_NUMBERS (foo)
  44. RELOC_NUMBER (R_foo_NONE, 0)
  45. RELOC_NUMBER (R_foo_32, 1)
  46. FAKE_RELOC (R_foo_illegal, 9)
  47. FAKE_RELOC (R_foo_synonym, 0)
  48. END_RELOC_NUMBERS (R_foo_count)
  49. will result in R_foo_count having a value of 1 (R_foo_synonym + 1)
  50. rather than 10 or 2 as might be expected.
  51. Alternatively you can assign a value to END_RELOC_NUMBERS symbol
  52. explicitly, like this:
  53. START_RELOC_NUMBERS (foo)
  54. RELOC_NUMBER (R_foo_NONE, 0)
  55. RELOC_NUMBER (R_foo_32, 1)
  56. FAKE_RELOC (R_foo_illegal, 9)
  57. FAKE_RELOC (R_foo_synonym, 0)
  58. END_RELOC_NUMBERS (R_foo_count = 2)
  59. If RELOC_MACROS_GEN_FUNC *is* defined, then instead the
  60. following function will be generated:
  61. static const char *foo (unsigned long rtype);
  62. static const char *
  63. foo (unsigned long rtype)
  64. {
  65. switch (rtype)
  66. {
  67. case 0: return "R_foo_NONE";
  68. case 1: return "R_foo_32";
  69. default: return NULL;
  70. }
  71. }
  72. */
  73. #ifndef _RELOC_MACROS_H
  74. #define _RELOC_MACROS_H
  75. #ifdef RELOC_MACROS_GEN_FUNC
  76. /* This function takes the relocation number and returns the
  77. string version name of the name of that relocation. If
  78. the relocation is not recognised, NULL is returned. */
  79. #define START_RELOC_NUMBERS(name) \
  80. static const char *name (unsigned long rtype); \
  81. static const char * \
  82. name (unsigned long rtype) \
  83. { \
  84. switch (rtype) \
  85. {
  86. #define RELOC_NUMBER(name, number) \
  87. case number: return #name;
  88. #define FAKE_RELOC(name, number)
  89. #define EMPTY_RELOC(name)
  90. #define END_RELOC_NUMBERS(name) \
  91. default: return NULL; \
  92. } \
  93. }
  94. #else /* Default to generating enum. */
  95. #define START_RELOC_NUMBERS(name) enum name {
  96. #define RELOC_NUMBER(name, number) name = number,
  97. #define FAKE_RELOC(name, number) name = number,
  98. #define EMPTY_RELOC(name) name,
  99. #define END_RELOC_NUMBERS(name) name };
  100. #endif
  101. #endif /* _RELOC_MACROS_H */