aboutsummaryrefslogtreecommitdiffstats
path: root/src/ortho/mcode/binary_file-macho.adb
blob: 610f56e45ffbf2edc2f9b5458a2c41396b52f1c7 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
--  Binary file Mach-O writer.
--  Copyright (C) 2015 Tristan Gingold
--
--  This program is free software: you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation, either version 2 of the License, or
--  (at your option) any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program.  If not, see <gnu.org/licenses>.
with Macho; use Macho;
with Macho_Arch64; use Macho_Arch64;

package body Binary_File.Macho is
   procedure Write (Fd : GNAT.OS_Lib.File_Descriptor)
   is
      use GNAT.OS_Lib;

      --  If true, discard local symbols;
      Flag_Discard_Local : Boolean := True;

      procedure Xwrite (Data : System.Address; Len : Natural) is
      begin
         if Write (Fd, Data, Len) /= Len then
            raise Write_Error;
         end if;
      end Xwrite;

      function Symbol_Discarded (S : Symbol) return Boolean is
      begin
         case Get_Scope (S) is
            when Sym_Local =>
               if Flag_Discard_Local then
                  return True;
               end if;
            when Sym_Private =>
               null;
            when Sym_Global =>
               null;
            when Sym_Undef =>
               if not Get_Used (S) then
                  return True;
               end if;
         end case;
         return False;
      end Symbol_Discarded;

      procedure Fill_Name (Dest : out String; Src : String)
      is
         subtype D_Type is String (1 .. Dest'Length);
         D : D_Type renames Dest;
         subtype S_Type is String (1 .. Src'Length);
         S : S_Type renames Src;
      begin
         if S'Length < D'Length then
            D (1 .. S'Length) := S;
            D (S'Length + 1 .. D'Last) := (others => ASCII.NUL);
         else
            D := S (1 .. D'Last);
         end if;
      end Fill_Name;

      type Section_Info_Type is record
         Sect : Section_Acc;
         --  Index of the section symbol (in symtab).
      end record;
      type Section_Info_Array is array (Natural range <>) of Section_Info_Type;
      Sects_Info : Section_Info_Array (1 .. Nbr_Sections);
      type Section_Array is array (Natural range <>) of Section;
      Sects_Hdr : Section_Array (1 .. Nbr_Sections);
      Nbr_Sect : Natural;
      Sect : Section_Acc;

      --  Various offsets.
      File_Offset : Natural;
      Seg_Offset : Natural;
      Symtab_Offset : Natural;
      Strtab_Offset : Natural;
      Sizeof_Cmds : Natural;

      --  Number of symtab entries.
      Nbr_Symbols : Natural;

      Str_Size : Natural;

      --  If true, do local relocs.
      Flag_Reloc : constant Boolean := True;
   begin
      --  If relocations are not performs, then local symbols cannot be
      --  discarded.
      if not Flag_Reloc then
         Flag_Discard_Local := False;
      end if;

      --  Count sections.
      Sect := Section_Chain;
      Nbr_Sect := 0;
      while Sect /= null loop
         Nbr_Sect := Nbr_Sect + 1;
         Sects_Info (Nbr_Sect).Sect := Sect;
         Sect.Number := Nbr_Sect;
         Sect := Sect.Next;
      end loop;

      --  Set sections offset.
      Sizeof_Cmds := Lc_Size + Segment_Command_Size
        + Nbr_Sect * Section_Size
        + Lc_Size + Symtab_Command_Size;
      File_Offset := Header_Size + Sizeof_Cmds;
      Seg_Offset := File_Offset;
      for I in 1 .. Nbr_Sect loop
         Sect := Sects_Info (I).Sect;
         if Sect.Data /= null then
            --  FIXME: alignment ?
            Sects_Hdr (I).Offset := Unsigned_32 (File_Offset);
            File_Offset := File_Offset + Natural (Sect.Pc);
         else
            Sects_Hdr (I).Offset := 0;
         end if;
      end loop;

      --  Relocs
      --  FIXME: todo.

      Symtab_Offset := File_Offset;
      Str_Size := 0;
      Nbr_Symbols := 0;
      for I in Symbols.First .. Symbols.Last loop
         if not Symbol_Discarded (I) then
            Nbr_Symbols := Nbr_Symbols + 1;
            Set_Number (I, Nbr_Symbols);
            Str_Size := Str_Size + Get_Symbol_Name_Length (I) + 1;
         else
            Set_Number (I, 0);
         end if;
      end loop;

      File_Offset := File_Offset + Nbr_Symbols * Nlist_Size;
      Strtab_Offset := File_Offset;

      --  Write file header.
      declare
         Hdr : Header;
         Cputype : Unsigned_32;
      begin
         case Arch is
            when Arch_X86 =>
               Cputype := Cputype_I386;
            when Arch_X86_64 =>
               Cputype := Cputype_I386 + Cpu_Arch_64;
            when others =>
               raise Program_Error;
         end case;
         Hdr := (Magic => Magic,
                 Cputype => Cputype,
                 Cpusubtype => Cpusubtype_I386_All,
                 Filetype => Mh_Object,
                 Ncmds => 2,
                 Sizeofcmds => Unsigned_32 (Sizeof_Cmds),
                 others => 0);
         Xwrite (Hdr'Address, Header_Size);
      end;

      --  Write segment and section commands.
      declare
         Lc : Load_Command;
         Seg : Segment_Command;
      begin
         Lc := (Cmd => Lc_Segment,
                Cmdsize => Unsigned_32 (Lc_Size + Segment_Command_Size
                                          + Nbr_Sect * Section_Size));
         Xwrite (Lc'Address, Lc_Size);
         Seg := (Segname => (others => ASCII.NUL),
                 Vmaddr => 0,
                 Vmsize => 0, --  FIXME
                 Fileoff => Addr_T (Seg_Offset),
                 Filesize => Addr_T (Symtab_Offset - Seg_Offset),
                 Maxprot => 7, --  rwx
                 Initprot => 7,
                 Nsects => Unsigned_32 (Nbr_Sect),
                 Flags => 0);
         Xwrite (Seg'Address, Segment_Command_Size);
      end;

      --  Write section headers.
      for I in 1 .. Nbr_Sect loop
         Sect := Sects_Info (I).Sect;
         declare
            Hdr : Section renames Sects_Hdr (I);
            Secname_Raw : constant String := Sect.Name.all;
            subtype S_Type is String (1 .. Secname_Raw'Length);
            Secname : S_Type renames Secname_Raw;
         begin
            if Secname = ".text" then
               Fill_Name (Hdr.Sectname, "__text");
               Fill_Name (Hdr.Segname, "__TEXT");
            elsif Secname = ".rodata" then
               Fill_Name (Hdr.Sectname, "__const");
               Fill_Name (Hdr.Segname, "__TEXT");
            elsif (Sect.Flags and Section_Debug) /= 0 then
               if Secname'Length > 7
                 and then Secname (1 .. 7) = ".debug_"
               then
                  Fill_Name (Hdr.Sectname,
                             "__debug_" & Secname (8 .. Secname'Last));
               else
                  Fill_Name (Hdr.Sectname, Sect.Name.all);
               end if;
               Fill_Name (Hdr.Segname, "__DWARF");
            else
               Fill_Name (Hdr.Sectname, Secname);
               Fill_Name (Hdr.Segname, "");
            end if;
            Hdr.Addr := Addr_T (Sect.Vaddr);
            Hdr.Size := Addr_T (Sect.Pc);
            Hdr.Align := Unsigned_32 (Sect.Align);
            Hdr.Reloff := 0;
            Hdr.Nreloc := 0;
            Hdr.Flags := 0;
            Hdr.Reserved1 := 0;
            Hdr.Reserved2 := 0;
            Xwrite (Hdr'Address, Section_Size);
         end;
      end loop;

      --  Write symtab command
      declare
         Lc : Load_Command;
         Symtab : Symtab_Command;
      begin
         Lc := (Cmd => Lc_Symtab,
                Cmdsize => Unsigned_32 (Lc_Size + Symtab_Command_Size));
         Xwrite (Lc'Address, Lc_Size);
         Symtab := (Symoff => Unsigned_32 (Symtab_Offset),
                    Nsyms => Unsigned_32 (Nbr_Symbols),
                    Stroff => Unsigned_32 (Strtab_Offset),
                    Strsize => Unsigned_32 (Str_Size));
         Xwrite (Symtab'Address, Symtab_Command_Size);
      end;

      --  Write sections content.
      for I in 1 .. Nbr_Sect loop
         Sect := Sects_Info (I).Sect;
         if Sect.Data /= null then
            Xwrite (Sect.Data (0)'Address, Natural (Sect.Pc));
         end if;
      end loop;

      --  FIXME: write relocs.

      --   Write symbols.
      declare
         Str_Offset : Natural;

         generic
            with procedure Handle (S : Symbol);
         procedure Foreach_Symbol;

         procedure Foreach_Symbol is
         begin
            --  First, the local and private symbols.
            for I in Symbols.First .. Symbols.Last loop
               case Get_Scope (I) is
                  when Sym_Local =>
                     if not Flag_Discard_Local then
                        Handle (I);
                     end if;
                  when Sym_Private =>
                     Handle (I);
                  when Sym_Global
                    | Sym_Undef =>
                     null;
               end case;
            end loop;

            --  Then global symbols
            for I in Symbols.First .. Symbols.Last loop
               case Get_Scope (I) is
                  when Sym_Local
                    | Sym_Private =>
                     null;
                  when Sym_Global =>
                     Handle (I);
                  when Sym_Undef =>
                     null;
               end case;
            end loop;
            --  Then undef symbols.
            for I in Symbols.First .. Symbols.Last loop
               case Get_Scope (I) is
                  when Sym_Local
                    | Sym_Private =>
                     null;
                  when Sym_Global =>
                     null;
                  when Sym_Undef =>
                     if Get_Used (I) then
                        Handle (I);
                     end if;
               end case;
            end loop;
         end Foreach_Symbol;

         procedure Write_Symbol (S : Symbol)
         is
            Sym : Nlist;
         begin
            Sym := (N_Strx => Unsigned_32 (Str_Offset),
                    N_Type => 0,
                    N_Sect => 0,
                    N_Desc => 0,
                    N_Value => Addr_T (Get_Symbol_Value (S)));
            Str_Offset := Str_Offset + Get_Symbol_Name_Length (S) + 1;
            if Get_Scope (S) = Sym_Undef then
               Sym.N_Type := N_Undf;
            else
               if Get_Scope (S) = Sym_Global then
                  Sym.N_Type := N_Sect + N_Ext;
               else
                  Sym.N_Type := N_Sect;
               end if;
               Sym.N_Sect := Unsigned_8 (Get_Section (S).Number);
               Sym.N_Value := Sym.N_Value + Addr_T (Get_Section (S).Vaddr);
            end if;
            Xwrite (Sym'Address, Nlist_Size);
         end Write_Symbol;

         procedure Write_String (Sym : Symbol)
         is
            Str : constant String := Get_Symbol_Name (Sym) & ASCII.NUL;
         begin
            Xwrite (Str'Address, Str'Length);
         end Write_String;

         procedure Write_All_Symbols is new
           Foreach_Symbol (Write_Symbol);
         procedure Write_All_Strings is new
           Foreach_Symbol (Write_String);
      begin
         Str_Offset := 0;

         Write_All_Symbols;
         Write_All_Strings;
      end;
   end Write;

end Binary_File.Macho;