summaryrefslogtreecommitdiff
path: root/mksyms.awk
blob: 167a05a809b759be4dc6cf312131b8736046236b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#
#  COPYRIGHT (c) 2012 Chris Johns <chrisj@rtems.org>
#
#  The license and distribution terms for this file may be
#  found in the file LICENSE in this distribution or at
#  http://www.rtems.com/license/LICENSE.
#
# Covert the list of symbols into a C structure.
#
#

function c_header()
{
  print ("/*");
  print (" * RTEMS Global Symbol Table");
  print (" *  Automatically generated. Do not edit, just regenerate.");
  print (" */");
  print ("");
  print ("#if __bfin__ || __h8300__ || __v850__");
  print ("extern unsigned char _rtems_rtl_base_globals[];");
  print ("extern unsigned int _rtems_rtl_base_globals_size;");
  print ("#else")
  print ("extern unsigned char __rtems_rtl_base_globals[];");
  print ("extern unsigned int __rtems_rtl_base_globals_size;");
  print ("#endif")
  print ("");
  print ("asm(\"  .align   4\");");
  print ("asm(\"__rtems_rtl_base_globals:\");");
}

function c_trailer()
{
  print ("asm(\"  .byte    0\");");
  print ("#if __mips__")
  print ("asm(\"  .align 0\");");
  print ("#else");
  print ("asm(\"  .balign 1\");");
  print ("#endif");
  print ("#if __mips__")
  print ("asm(\"  .align 0\");");
  print ("#else");
  print ("asm(\"  .balign 1\");");
  print ("#endif");
  print ("asm(\"  .ascii \\\"\\xde\\xad\\xbe\\xef\\\"\");");
  print ("asm(\"  .align   4\");");
  print ("asm(\"__rtems_rtl_base_globals_size:\");");
  print ("asm(\"  .long __rtems_rtl_base_globals_size - __rtems_rtl_base_globals\");");
  print ("");
  print ("void rtems_rtl_base_sym_global_add (const unsigned char* , unsigned int );");
}

function c_rtl_call_body()
{
  print ("{");
  print ("#if __bfin__ || __h8300__ || __v850__")
  print ("  rtems_rtl_base_sym_global_add (_rtems_rtl_base_globals,");
  print ("                                 _rtems_rtl_base_globals_size);");
  print ("#else")
  print ("  rtems_rtl_base_sym_global_add (__rtems_rtl_base_globals,");
  print ("                                 __rtems_rtl_base_globals_size);");
  print ("#endif")
  print ("}");
}

function c_constructor_trailer()
{
  c_trailer();
  print ("static void init(void) __attribute__ ((constructor));");
  print ("static void init(void)");
  c_rtl_call_body();
}

function c_embedded_trailer()
{
  c_trailer();
  print ("void rtems_rtl_base_global_syms_init(void);");
  print ("void rtems_rtl_base_global_syms_init(void)");
  c_rtl_call_body();
}

BEGIN {
  FS = "[ \t\n]";
  OFS = " ";
  started = 0
  embed = 1
  for (a = 0; a < ARGC; ++a)
  {
    if (ARGV[a] == "--no-embed")
    {
      embed = 0
      delete ARGV[a];
    }
    else if (ARGV[a] != "-")
    {
      ap = index (ARGV[a], "awk")
      if ((ap == 0) || (ap != (length(ARGV[a]) - 2)))
      {
        print ("invalid option:", ARGV[a]);
        exit 2
      }
    }
  }
  c_header();
  syms = 0
  started = 1
}
END {
  if (started)
  {
    for (s = 0; s < syms; ++s)
    {
      printf ("asm(\"  .asciz \\\"%s\\\"\");\n", symbols[s]);
      if (embed)
      {
        print ("#if __mips__")
        print ("asm(\"  .align 0\");");
        print ("#else");
        print ("asm(\"  .balign 1\");");
        print ("#endif");
        printf ("asm(\"  .long %s\");\n", symbols[s]);
      }
      else
        printf ("asm(\"  .long 0x%s\");\n", addresses[s]);
    }
    if (embed)
      c_embedded_trailer();
    else
      c_constructor_trailer();
  }
}

#
# Parse the output of 'nm'
#

{
  #
  # We need 3 fields
  #
  if (NF == 3)
  {
    if (($3 != "__DTOR_END__") ||
        ($3 != "__CTOR_END__") ||
        ($3 != "__DYNAMIC"))
    {
      symbols[syms] = $3;
      addresses[syms] = $1;
      ++syms;
    }
  }
}