1/* Return section type name.
2   Copyright (C) 2001, 2002, 2006, 2008 Red Hat, Inc.
3   This file is part of elfutils.
4   Written by Ulrich Drepper <drepper@redhat.com>, 2001.
5
6   This file is free software; you can redistribute it and/or modify
7   it under the terms of either
8
9     * the GNU Lesser General Public License as published by the Free
10       Software Foundation; either version 3 of the License, or (at
11       your option) any later version
12
13   or
14
15     * the GNU General Public License as published by the Free
16       Software Foundation; either version 2 of the License, or (at
17       your option) any later version
18
19   or both in parallel, as here.
20
21   elfutils is distributed in the hope that it will be useful, but
22   WITHOUT ANY WARRANTY; without even the implied warranty of
23   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24   General Public License for more details.
25
26   You should have received copies of the GNU General Public License and
27   the GNU Lesser General Public License along with this program.  If
28   not, see <http://www.gnu.org/licenses/>.  */
29
30#ifdef HAVE_CONFIG_H
31# include <config.h>
32#endif
33
34#include <stdio.h>
35#include <libeblP.h>
36
37
38const char *
39ebl_section_type_name (Ebl *ebl, int section, char *buf, size_t len)
40{
41  const char *res = ebl->section_type_name (section, buf, len);
42
43  if (res == NULL)
44    {
45      static const char *knowntypes[] =
46	{
47#define KNOWNSTYPE(name) [SHT_##name] = #name
48	  KNOWNSTYPE (NULL),
49	  KNOWNSTYPE (PROGBITS),
50	  KNOWNSTYPE (SYMTAB),
51	  KNOWNSTYPE (STRTAB),
52	  KNOWNSTYPE (RELA),
53	  KNOWNSTYPE (HASH),
54	  KNOWNSTYPE (DYNAMIC),
55	  KNOWNSTYPE (NOTE),
56	  KNOWNSTYPE (NOBITS),
57	  KNOWNSTYPE (REL),
58	  KNOWNSTYPE (SHLIB),
59	  KNOWNSTYPE (DYNSYM),
60	  KNOWNSTYPE (INIT_ARRAY),
61	  KNOWNSTYPE (FINI_ARRAY),
62	  KNOWNSTYPE (PREINIT_ARRAY),
63	  KNOWNSTYPE (GROUP),
64	  KNOWNSTYPE (SYMTAB_SHNDX)
65	};
66
67      /* Handle standard names.  */
68      if ((size_t) section < sizeof (knowntypes) / sizeof (knowntypes[0])
69	  && knowntypes[section] != NULL)
70	res = knowntypes[section];
71      /* The symbol versioning/Sun extensions.  */
72      else if (section >= SHT_LOSUNW && section <= SHT_HISUNW)
73	{
74	  static const char *sunwtypes[] =
75	    {
76#undef KNOWNSTYPE
77#define KNOWNSTYPE(name) [SHT_##name - SHT_LOSUNW] = #name
78	      KNOWNSTYPE (SUNW_move),
79	      KNOWNSTYPE (SUNW_COMDAT),
80	      KNOWNSTYPE (SUNW_syminfo),
81	      KNOWNSTYPE (GNU_verdef),
82	      KNOWNSTYPE (GNU_verneed),
83	      KNOWNSTYPE (GNU_versym)
84	    };
85	  res = sunwtypes[section - SHT_LOSUNW];
86	}
87      else
88	/* A few GNU additions.  */
89	switch (section)
90	  {
91	  case SHT_CHECKSUM:
92	    res = "CHECKSUM";
93	    break;
94	  case SHT_GNU_LIBLIST:
95	    res = "GNU_LIBLIST";
96	    break;
97	  case SHT_GNU_HASH:
98	    res = "GNU_HASH";
99	    break;
100	  case SHT_GNU_ATTRIBUTES:
101	    res = "GNU_ATTRIBUTES";
102	    break;
103
104	  default:
105	    /* Handle OS-specific section names.  */
106	    if (section >= SHT_LOOS && section <= SHT_HIOS)
107	      snprintf (buf, len, "SHT_LOOS+%x", section - SHT_LOOS);
108	    /* Handle processor-specific section names.  */
109	    else if (section >= SHT_LOPROC && section <= SHT_HIPROC)
110	      snprintf (buf, len, "SHT_LOPROC+%x", section - SHT_LOPROC);
111	    else if ((unsigned int) section >= SHT_LOUSER
112		     && (unsigned int) section <= SHT_HIUSER)
113	      snprintf (buf, len, "SHT_LOUSER+%x", section - SHT_LOUSER);
114	    else
115	      snprintf (buf, len, "%s: %d", gettext ("<unknown>"), section);
116
117	    res = buf;
118	    break;
119	  }
120    }
121
122  return res;
123}
124