160484Sobrien/* Generic relocation support for BFD.
2130561Sobrien   Copyright 1998, 1999, 2000, 2003 Free Software Foundation, Inc.
360484Sobrien
460484Sobrien   This file is part of BFD, the Binary File Descriptor library.
560484Sobrien
660484Sobrien   This program is free software; you can redistribute it and/or modify
760484Sobrien   it under the terms of the GNU General Public License as published by
860484Sobrien   the Free Software Foundation; either version 2 of the License, or
960484Sobrien   (at your option) any later version.
1060484Sobrien
1160484Sobrien   This program is distributed in the hope that it will be useful,
1260484Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1360484Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1460484Sobrien   GNU General Public License for more details.
1560484Sobrien
1660484Sobrien   You should have received a copy of the GNU General Public License
1760484Sobrien   along with this program; if not, write to the Free Software Foundation,
18218822Sdim   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
1960484Sobrien
2060484Sobrien/* These macros are used by the various *.h target specific header
2160484Sobrien   files to either generate an enum containing all the known relocations
2260484Sobrien   for that target, or if RELOC_MACROS_GEN_FUNC is defined, a recognition
2360484Sobrien   function is generated instead.  (This is used by binutils/readelf.c)
2460484Sobrien
2560484Sobrien   Given a header file like this:
2660484Sobrien
2760484Sobrien   	START_RELOC_NUMBERS (foo)
2860484Sobrien   	    RELOC_NUMBER (R_foo_NONE,    0)
2960484Sobrien   	    RELOC_NUMBER (R_foo_32,      1)
3077298Sobrien   	    EMPTY_RELOC  (R_foo_good)
3177298Sobrien   	    FAKE_RELOC   (R_foo_illegal, 9)
3277298Sobrien   	END_RELOC_NUMBERS (R_foo_count)
3360484Sobrien
3460484Sobrien   Then the following will be produced by default (ie if
3560484Sobrien   RELOC_MACROS_GEN_FUNC is *not* defined).
3660484Sobrien
3760484Sobrien   	enum foo
3860484Sobrien	{
3960484Sobrien   	  R_foo_NONE = 0,
4060484Sobrien   	  R_foo_32 = 1,
4177298Sobrien	  R_foo_good,
4277298Sobrien   	  R_foo_illegal = 9,
4377298Sobrien   	  R_foo_count
4460484Sobrien   	};
4560484Sobrien
4660484Sobrien   If RELOC_MACROS_GEN_FUNC *is* defined, then instead the
4760484Sobrien   following function will be generated:
4860484Sobrien
49130561Sobrien   	static const char *foo (unsigned long rtype);
5060484Sobrien   	static const char *
51130561Sobrien   	foo (unsigned long rtype)
5260484Sobrien   	{
5360484Sobrien   	   switch (rtype)
5460484Sobrien   	   {
5560484Sobrien   	   case 0: return "R_foo_NONE";
5660484Sobrien   	   case 1: return "R_foo_32";
5760484Sobrien   	   default: return NULL;
5860484Sobrien   	   }
5960484Sobrien   	}
6060484Sobrien   */
6177298Sobrien
6260484Sobrien#ifndef _RELOC_MACROS_H
6360484Sobrien#define _RELOC_MACROS_H
6460484Sobrien
6560484Sobrien#ifdef RELOC_MACROS_GEN_FUNC
6660484Sobrien
6760484Sobrien/* This function takes the relocation number and returns the
6860484Sobrien   string version name of the name of that relocation.  If
6960484Sobrien   the relocation is not recognised, NULL is returned.  */
7060484Sobrien
7160484Sobrien#define START_RELOC_NUMBERS(name)   				\
72130561Sobrienstatic const char *name (unsigned long rtype);			\
7360484Sobrienstatic const char *						\
74130561Sobrienname (unsigned long rtype)					\
7560484Sobrien{								\
7660484Sobrien  switch (rtype)						\
77130561Sobrien    {
7860484Sobrien
79130561Sobrien#define RELOC_NUMBER(name, number) \
80130561Sobrien    case number: return #name;
8160484Sobrien
8277298Sobrien#define FAKE_RELOC(name, number)
8360484Sobrien#define EMPTY_RELOC(name)
8477298Sobrien
8577298Sobrien#define END_RELOC_NUMBERS(name)	\
8660484Sobrien    default: return NULL;	\
87130561Sobrien    }				\
8860484Sobrien}
8960484Sobrien
9060484Sobrien
9160484Sobrien#else /* Default to generating enum.  */
9260484Sobrien
9377298Sobrien#define START_RELOC_NUMBERS(name)   enum name {
9477298Sobrien#define RELOC_NUMBER(name, number)  name = number,
9577298Sobrien#define FAKE_RELOC(name, number)    name = number,
9677298Sobrien#define EMPTY_RELOC(name)           name,
9777298Sobrien#define END_RELOC_NUMBERS(name)     name };
9860484Sobrien
9960484Sobrien#endif
10060484Sobrien
101218822Sdim#endif /* _RELOC_MACROS_H */
102