-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlltool.c
3736 lines (3145 loc) · 92.9 KB
/
dlltool.c
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* dlltool.c -- tool to generate stuff for PE style DLLs
Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
Free Software Foundation, Inc.
This file is part of GNU Binutils.
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA. */
/* This program allows you to build the files necessary to create
DLLs to run on a system which understands PE format image files.
(eg, Windows NT)
See "Peering Inside the PE: A Tour of the Win32 Portable Executable
File Format", MSJ 1994, Volume 9 for more information.
Also see "Microsoft Portable Executable and Common Object File Format,
Specification 4.1" for more information.
A DLL contains an export table which contains the information
which the runtime loader needs to tie up references from a
referencing program.
The export table is generated by this program by reading
in a .DEF file or scanning the .a and .o files which will be in the
DLL. A .o file can contain information in special ".drectve" sections
with export information.
A DEF file contains any number of the following commands:
NAME <name> [ , <base> ]
The result is going to be <name>.EXE
LIBRARY <name> [ , <base> ]
The result is going to be <name>.DLL
EXPORTS ( ( ( <name1> [ = <name2> ] )
| ( <name1> = <module-name> . <external-name>))
[ @ <integer> ] [ NONAME ] [CONSTANT] [DATA] ) *
Declares name1 as an exported symbol from the
DLL, with optional ordinal number <integer>.
Or declares name1 as an alias (forward) of the function <external-name>
in the DLL <module-name>.
IMPORTS ( ( <internal-name> = <module-name> . <integer> )
| ( [ <internal-name> = ] <module-name> . <external-name> )) *
Declares that <external-name> or the exported function whoes ordinal number
is <integer> is to be imported from the file <module-name>. If
<internal-name> is specified then this is the name that the imported
function will be refered to in the body of the DLL.
DESCRIPTION <string>
Puts <string> into output .exp file in the .rdata section
[STACKSIZE|HEAPSIZE] <number-reserve> [ , <number-commit> ]
Generates --stack|--heap <number-reserve>,<number-commit>
in the output .drectve section. The linker will
see this and act upon it.
[CODE|DATA] <attr>+
SECTIONS ( <sectionname> <attr>+ )*
<attr> = READ | WRITE | EXECUTE | SHARED
Generates --attr <sectionname> <attr> in the output
.drectve section. The linker will see this and act
upon it.
A -export:<name> in a .drectve section in an input .o or .a
file to this program is equivalent to a EXPORTS <name>
in a .DEF file.
The program generates output files with the prefix supplied
on the command line, or in the def file, or taken from the first
supplied argument.
The .exp.s file contains the information necessary to export
the routines in the DLL. The .lib.s file contains the information
necessary to use the DLL's routines from a referencing program.
Example:
file1.c:
asm (".section .drectve");
asm (".ascii \"-export:adef\"");
void adef (char * s)
{
printf ("hello from the dll %s\n", s);
}
void bdef (char * s)
{
printf ("hello from the dll and the other entry point %s\n", s);
}
file2.c:
asm (".section .drectve");
asm (".ascii \"-export:cdef\"");
asm (".ascii \"-export:ddef\"");
void cdef (char * s)
{
printf ("hello from the dll %s\n", s);
}
void ddef (char * s)
{
printf ("hello from the dll and the other entry point %s\n", s);
}
int printf (void)
{
return 9;
}
themain.c:
int main (void)
{
cdef ();
return 0;
}
thedll.def
LIBRARY thedll
HEAPSIZE 0x40000, 0x2000
EXPORTS bdef @ 20
cdef @ 30 NONAME
SECTIONS donkey READ WRITE
aardvark EXECUTE
# Compile up the parts of the dll and the program
gcc -c file1.c file2.c themain.c
# Optional: put the dll objects into a library
# (you don't have to, you could name all the object
# files on the dlltool line)
ar qcv thedll.in file1.o file2.o
ranlib thedll.in
# Run this tool over the DLL's .def file and generate an exports
# file (thedll.o) and an imports file (thedll.a).
# (You may have to use -S to tell dlltool where to find the assembler).
dlltool --def thedll.def --output-exp thedll.o --output-lib thedll.a
# Build the dll with the library and the export table
ld -o thedll.dll thedll.o thedll.in
# Link the executable with the import library
gcc -o themain.exe themain.o thedll.a
This example can be extended if relocations are needed in the DLL:
# Compile up the parts of the dll and the program
gcc -c file1.c file2.c themain.c
# Run this tool over the DLL's .def file and generate an imports file.
dlltool --def thedll.def --output-lib thedll.lib
# Link the executable with the import library and generate a base file
# at the same time
gcc -o themain.exe themain.o thedll.lib -Wl,--base-file -Wl,themain.base
# Run this tool over the DLL's .def file and generate an exports file
# which includes the relocations from the base file.
dlltool --def thedll.def --base-file themain.base --output-exp thedll.exp
# Build the dll with file1.o, file2.o and the export table
ld -o thedll.dll thedll.exp file1.o file2.o */
/* .idata section description
The .idata section is the import table. It is a collection of several
subsections used to keep the pieces for each dll together: .idata$[234567].
IE: Each dll's .idata$2's are catenated together, each .idata$3's, etc.
.idata$2 = Import Directory Table
= array of IMAGE_IMPORT_DESCRIPTOR's.
DWORD Import Lookup Table; - pointer to .idata$4
DWORD TimeDateStamp; - currently always 0
DWORD ForwarderChain; - currently always 0
DWORD Name; - pointer to dll's name
PIMAGE_THUNK_DATA FirstThunk; - pointer to .idata$5
.idata$3 = null terminating entry for .idata$2.
.idata$4 = Import Lookup Table
= array of array of pointers to hint name table.
There is one for each dll being imported from, and each dll's set is
terminated by a trailing NULL.
.idata$5 = Import Address Table
= array of array of pointers to hint name table.
There is one for each dll being imported from, and each dll's set is
terminated by a trailing NULL.
Initially, this table is identical to the Import Lookup Table. However,
at load time, the loader overwrites the entries with the address of the
function.
.idata$6 = Hint Name Table
= Array of { short, asciz } entries, one for each imported function.
The `short' is the function's ordinal number.
.idata$7 = dll name (eg: "kernel32.dll"). (.idata$6 for ppc). */
/* AIX requires this to be the first thing in the file. */
#ifndef __GNUC__
# ifdef _AIX
#pragma alloca
#endif
#endif
#define show_allnames 0
#define PAGE_SIZE 4096
#define PAGE_MASK (-PAGE_SIZE)
#include "bfd.h"
#include "libiberty.h"
#include "bucomm.h"
#include "getopt.h"
#include "demangle.h"
#include "dyn-string.h"
#include "dlltool.h"
#include "safe-ctype.h"
#include <time.h>
#include <sys/stat.h>
#ifdef ANSI_PROTOTYPES
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#ifdef DLLTOOL_ARM
#include "coff/arm.h"
#include "coff/internal.h"
#endif
/* Forward references. */
static char *look_for_prog
PARAMS ((const char *, const char *, int));
static char *deduce_name
PARAMS ((const char *));
#ifdef DLLTOOL_MCORE_ELF
static void mcore_elf_cache_filename
PARAMS ((char *));
static void mcore_elf_gen_out_file
PARAMS ((void));
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#else /* ! HAVE_SYS_WAIT_H */
#if ! defined (_WIN32) || defined (__CYGWIN32__)
#ifndef WIFEXITED
#define WIFEXITED(w) (((w) & 0377) == 0)
#endif
#ifndef WIFSIGNALED
#define WIFSIGNALED(w) (((w) & 0377) != 0177 && ((w) & ~0377) == 0)
#endif
#ifndef WTERMSIG
#define WTERMSIG(w) ((w) & 0177)
#endif
#ifndef WEXITSTATUS
#define WEXITSTATUS(w) (((w) >> 8) & 0377)
#endif
#else /* defined (_WIN32) && ! defined (__CYGWIN32__) */
#ifndef WIFEXITED
#define WIFEXITED(w) (((w) & 0xff) == 0)
#endif
#ifndef WIFSIGNALED
#define WIFSIGNALED(w) (((w) & 0xff) != 0 && ((w) & 0xff) != 0x7f)
#endif
#ifndef WTERMSIG
#define WTERMSIG(w) ((w) & 0x7f)
#endif
#ifndef WEXITSTATUS
#define WEXITSTATUS(w) (((w) & 0xff00) >> 8)
#endif
#endif /* defined (_WIN32) && ! defined (__CYGWIN32__) */
#endif /* ! HAVE_SYS_WAIT_H */
/* ifunc and ihead data structures: [email protected] 1997
When IMPORT declarations are encountered in a .def file the
function import information is stored in a structure referenced by
the global variable IMPORT_LIST. The structure is a linked list
containing the names of the dll files each function is imported
from and a linked list of functions being imported from that dll
file. This roughly parallels the structure of the .idata section
in the PE object file.
The contents of .def file are interpreted from within the
process_def_file function. Every time an IMPORT declaration is
encountered, it is broken up into its component parts and passed to
def_import. IMPORT_LIST is initialized to NULL in function main. */
typedef struct ifunct
{
char * name; /* Name of function being imported. */
int ord; /* Two-byte ordinal value associated with function. */
struct ifunct *next;
} ifunctype;
typedef struct iheadt
{
char *dllname; /* Name of dll file imported from. */
long nfuncs; /* Number of functions in list. */
struct ifunct *funchead; /* First function in list. */
struct ifunct *functail; /* Last function in list. */
struct iheadt *next; /* Next dll file in list. */
} iheadtype;
/* Structure containing all import information as defined in .def file
(qv "ihead structure"). */
static iheadtype *import_list = NULL;
static char *as_name = NULL;
static char * as_flags = "";
static char *tmp_prefix = "d";
static int no_idata4;
static int no_idata5;
static char *exp_name;
static char *imp_name;
static char *head_label;
static char *imp_name_lab;
static char *dll_name;
static int add_indirect = 0;
static int add_underscore = 0;
static int dontdeltemps = 0;
/* TRUE if we should export all symbols. Otherwise, we only export
symbols listed in .drectve sections or in the def file. */
static bfd_boolean export_all_symbols;
/* TRUE if we should exclude the symbols in DEFAULT_EXCLUDES when
exporting all symbols. */
static bfd_boolean do_default_excludes = TRUE;
/* Default symbols to exclude when exporting all the symbols. */
static const char *default_excludes = "DllMain@12,DllEntryPoint@0,impure_ptr";
/* TRUE if we should add __imp_<SYMBOL> to import libraries for backward
compatibility to old Cygwin releases. */
static bfd_boolean create_compat_implib;
static char *def_file;
extern char * program_name;
static int machine;
static int killat;
static int add_stdcall_alias;
static int verbose;
static FILE *output_def;
static FILE *base_file;
#ifdef DLLTOOL_ARM
#ifdef DLLTOOL_ARM_EPOC
static const char *mname = "arm-epoc";
#else
static const char *mname = "arm";
#endif
#endif
#ifdef DLLTOOL_I386
static const char *mname = "i386";
#endif
#ifdef DLLTOOL_PPC
static const char *mname = "ppc";
#endif
#ifdef DLLTOOL_SH
static const char *mname = "sh";
#endif
#ifdef DLLTOOL_MIPS
static const char *mname = "mips";
#endif
#ifdef DLLTOOL_MCORE
static const char * mname = "mcore-le";
#endif
#ifdef DLLTOOL_MCORE_ELF
static const char * mname = "mcore-elf";
static char * mcore_elf_out_file = NULL;
static char * mcore_elf_linker = NULL;
static char * mcore_elf_linker_flags = NULL;
#define DRECTVE_SECTION_NAME ((machine == MMCORE_ELF || machine == MMCORE_ELF_LE) ? ".exports" : ".drectve")
#endif
#ifndef DRECTVE_SECTION_NAME
#define DRECTVE_SECTION_NAME ".drectve"
#endif
#define PATHMAX 250 /* What's the right name for this ? */
char *tmp_asm_buf;
char *tmp_head_s_buf;
char *tmp_head_o_buf;
char *tmp_tail_s_buf;
char *tmp_tail_o_buf;
char *tmp_stub_buf;
#define TMP_ASM dlltmp (tmp_asm_buf, "%sc.s")
#define TMP_HEAD_S dlltmp (tmp_head_s_buf, "%sh.s")
#define TMP_HEAD_O dlltmp (tmp_head_o_buf, "%sh.o")
#define TMP_TAIL_S dlltmp (tmp_tail_s_buf, "%st.s")
#define TMP_TAIL_O dlltmp (tmp_tail_o_buf, "%st.o")
#define TMP_STUB dlltmp (tmp_stub_buf, "%ss")
/* This bit of assemly does jmp * .... */
static const unsigned char i386_jtab[] =
{
0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90
};
static const unsigned char arm_jtab[] =
{
0x00, 0xc0, 0x9f, 0xe5, /* ldr ip, [pc] */
0x00, 0xf0, 0x9c, 0xe5, /* ldr pc, [ip] */
0, 0, 0, 0
};
static const unsigned char arm_interwork_jtab[] =
{
0x04, 0xc0, 0x9f, 0xe5, /* ldr ip, [pc] */
0x00, 0xc0, 0x9c, 0xe5, /* ldr ip, [ip] */
0x1c, 0xff, 0x2f, 0xe1, /* bx ip */
0, 0, 0, 0
};
static const unsigned char thumb_jtab[] =
{
0x40, 0xb4, /* push {r6} */
0x02, 0x4e, /* ldr r6, [pc, #8] */
0x36, 0x68, /* ldr r6, [r6] */
0xb4, 0x46, /* mov ip, r6 */
0x40, 0xbc, /* pop {r6} */
0x60, 0x47, /* bx ip */
0, 0, 0, 0
};
static const unsigned char mcore_be_jtab[] =
{
0x71, 0x02, /* lrw r1,2 */
0x81, 0x01, /* ld.w r1,(r1,0) */
0x00, 0xC1, /* jmp r1 */
0x12, 0x00, /* nop */
0x00, 0x00, 0x00, 0x00 /* <address> */
};
static const unsigned char mcore_le_jtab[] =
{
0x02, 0x71, /* lrw r1,2 */
0x01, 0x81, /* ld.w r1,(r1,0) */
0xC1, 0x00, /* jmp r1 */
0x00, 0x12, /* nop */
0x00, 0x00, 0x00, 0x00 /* <address> */
};
/* This is the glue sequence for PowerPC PE. There is a
tocrel16-tocdefn reloc against the first instruction.
We also need a IMGLUE reloc against the glue function
to restore the toc saved by the third instruction in
the glue. */
static const unsigned char ppc_jtab[] =
{
0x00, 0x00, 0x62, 0x81, /* lwz r11,0(r2) */
/* Reloc TOCREL16 __imp_xxx */
0x00, 0x00, 0x8B, 0x81, /* lwz r12,0(r11) */
0x04, 0x00, 0x41, 0x90, /* stw r2,4(r1) */
0xA6, 0x03, 0x89, 0x7D, /* mtctr r12 */
0x04, 0x00, 0x4B, 0x80, /* lwz r2,4(r11) */
0x20, 0x04, 0x80, 0x4E /* bctr */
};
#ifdef DLLTOOL_PPC
/* The glue instruction, picks up the toc from the stw in
the above code: "lwz r2,4(r1)". */
static bfd_vma ppc_glue_insn = 0x80410004;
#endif
struct mac
{
const char *type;
const char *how_byte;
const char *how_short;
const char *how_long;
const char *how_asciz;
const char *how_comment;
const char *how_jump;
const char *how_global;
const char *how_space;
const char *how_align_short;
const char *how_align_long;
const char *how_default_as_switches;
const char *how_bfd_target;
enum bfd_architecture how_bfd_arch;
const unsigned char *how_jtab;
int how_jtab_size; /* Size of the jtab entry. */
int how_jtab_roff; /* Offset into it for the ind 32 reloc into idata 5. */
};
static const struct mac
mtable[] =
{
{
#define MARM 0
"arm", ".byte", ".short", ".long", ".asciz", "@",
"ldr\tip,[pc]\n\tldr\tpc,[ip]\n\t.long",
".global", ".space", ".align\t2",".align\t4", "-mapcs-32",
"pe-arm-little", bfd_arch_arm,
arm_jtab, sizeof (arm_jtab), 8
}
,
{
#define M386 1
"i386", ".byte", ".short", ".long", ".asciz", "#",
"jmp *", ".global", ".space", ".align\t2",".align\t4", "",
"pe-i386",bfd_arch_i386,
i386_jtab, sizeof (i386_jtab), 2
}
,
{
#define MPPC 2
"ppc", ".byte", ".short", ".long", ".asciz", "#",
"jmp *", ".global", ".space", ".align\t2",".align\t4", "",
"pe-powerpcle",bfd_arch_powerpc,
ppc_jtab, sizeof (ppc_jtab), 0
}
,
{
#define MTHUMB 3
"thumb", ".byte", ".short", ".long", ".asciz", "@",
"push\t{r6}\n\tldr\tr6, [pc, #8]\n\tldr\tr6, [r6]\n\tmov\tip, r6\n\tpop\t{r6}\n\tbx\tip",
".global", ".space", ".align\t2",".align\t4", "-mthumb-interwork",
"pe-arm-little", bfd_arch_arm,
thumb_jtab, sizeof (thumb_jtab), 12
}
,
#define MARM_INTERWORK 4
{
"arm_interwork", ".byte", ".short", ".long", ".asciz", "@",
"ldr\tip,[pc]\n\tldr\tip,[ip]\n\tbx\tip\n\t.long",
".global", ".space", ".align\t2",".align\t4", "-mthumb-interwork",
"pe-arm-little", bfd_arch_arm,
arm_interwork_jtab, sizeof (arm_interwork_jtab), 12
}
,
{
#define MMCORE_BE 5
"mcore-be", ".byte", ".short", ".long", ".asciz", "//",
"lrw r1,[1f]\n\tld.w r1,(r1,0)\n\tjmp r1\n\tnop\n1:.long",
".global", ".space", ".align\t2",".align\t4", "",
"pe-mcore-big", bfd_arch_mcore,
mcore_be_jtab, sizeof (mcore_be_jtab), 8
}
,
{
#define MMCORE_LE 6
"mcore-le", ".byte", ".short", ".long", ".asciz", "//",
"lrw r1,[1f]\n\tld.w r1,(r1,0)\n\tjmp r1\n\tnop\n1:.long",
".global", ".space", ".align\t2",".align\t4", "-EL",
"pe-mcore-little", bfd_arch_mcore,
mcore_le_jtab, sizeof (mcore_le_jtab), 8
}
,
{
#define MMCORE_ELF 7
"mcore-elf-be", ".byte", ".short", ".long", ".asciz", "//",
"lrw r1,[1f]\n\tld.w r1,(r1,0)\n\tjmp r1\n\tnop\n1:.long",
".global", ".space", ".align\t2",".align\t4", "",
"elf32-mcore-big", bfd_arch_mcore,
mcore_be_jtab, sizeof (mcore_be_jtab), 8
}
,
{
#define MMCORE_ELF_LE 8
"mcore-elf-le", ".byte", ".short", ".long", ".asciz", "//",
"lrw r1,[1f]\n\tld.w r1,(r1,0)\n\tjmp r1\n\tnop\n1:.long",
".global", ".space", ".align\t2",".align\t4", "-EL",
"elf32-mcore-little", bfd_arch_mcore,
mcore_le_jtab, sizeof (mcore_le_jtab), 8
}
,
{
#define MARM_EPOC 9
"arm-epoc", ".byte", ".short", ".long", ".asciz", "@",
"ldr\tip,[pc]\n\tldr\tpc,[ip]\n\t.long",
".global", ".space", ".align\t2",".align\t4", "",
"epoc-pe-arm-little", bfd_arch_arm,
arm_jtab, sizeof (arm_jtab), 8
}
,
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
typedef struct dlist
{
char *text;
struct dlist *next;
}
dlist_type;
typedef struct export
{
const char *name;
const char *internal_name;
int ordinal;
int constant;
int noname;
int data;
int hint;
int forward; /* Number of forward label, 0 means no forward. */
struct export *next;
}
export_type;
/* A list of symbols which we should not export. */
struct string_list
{
struct string_list *next;
char *string;
};
static struct string_list *excludes;
static const char *rvaafter
PARAMS ((int));
static const char *rvabefore
PARAMS ((int));
static const char *asm_prefix
PARAMS ((int));
static void process_def_file
PARAMS ((const char *));
static void new_directive
PARAMS ((char *));
static void append_import
PARAMS ((const char *, const char *, int));
static void run
PARAMS ((const char *, char *));
static void scan_drectve_symbols
PARAMS ((bfd *));
static void scan_filtered_symbols
PARAMS ((bfd *, PTR, long, unsigned int));
static void add_excludes
PARAMS ((const char *));
static bfd_boolean match_exclude
PARAMS ((const char *));
static void set_default_excludes
PARAMS ((void));
static long filter_symbols
PARAMS ((bfd *, PTR, long, unsigned int));
static void scan_all_symbols
PARAMS ((bfd *));
static void scan_open_obj_file
PARAMS ((bfd *));
static void scan_obj_file
PARAMS ((const char *));
static void dump_def_info
PARAMS ((FILE *));
static int sfunc
PARAMS ((const void *, const void *));
static void flush_page
PARAMS ((FILE *, long *, int, int));
static void gen_def_file
PARAMS ((void));
static void generate_idata_ofile
PARAMS ((FILE *));
static void assemble_file
PARAMS ((const char *, const char *));
static void gen_exp_file
PARAMS ((void));
static const char *xlate
PARAMS ((const char *));
#if 0
static void dump_iat
PARAMS ((FILE *, export_type *));
#endif
static char *make_label
PARAMS ((const char *, const char *));
static char *make_imp_label
PARAMS ((const char *, const char *));
static bfd *make_one_lib_file
PARAMS ((export_type *, int));
static bfd *make_head
PARAMS ((void));
static bfd *make_tail
PARAMS ((void));
static void gen_lib_file
PARAMS ((void));
static int pfunc
PARAMS ((const void *, const void *));
static int nfunc
PARAMS ((const void *, const void *));
static void remove_null_names
PARAMS ((export_type **));
static void dtab
PARAMS ((export_type **));
static void process_duplicates
PARAMS ((export_type **));
static void fill_ordinals
PARAMS ((export_type **));
static int alphafunc
PARAMS ((const void *, const void *));
static void mangle_defs
PARAMS ((void));
static void usage
PARAMS ((FILE *, int));
static void inform
PARAMS ((const char *, ...));
static char *
dlltmp PARAMS ((char *buf, const char *fmt))
{
if (!buf)
buf = malloc (strlen (tmp_prefix) + 17);
sprintf (buf, fmt, tmp_prefix);
return buf;
}
static void
inform VPARAMS ((const char * message, ...))
{
VA_OPEN (args, message);
VA_FIXEDARG (args, const char *, message);
if (!verbose)
return;
report (message, args);
VA_CLOSE (args);
}
static const char *
rvaafter (machine)
int machine;
{
switch (machine)
{
case MARM:
case M386:
case MPPC:
case MTHUMB:
case MARM_INTERWORK:
case MMCORE_BE:
case MMCORE_LE:
case MMCORE_ELF:
case MMCORE_ELF_LE:
case MARM_EPOC:
break;
default:
/* xgettext:c-format */
fatal (_("Internal error: Unknown machine type: %d"), machine);
break;
}
return "";
}
static const char *
rvabefore (machine)
int machine;
{
switch (machine)
{
case MARM:
case M386:
case MPPC:
case MTHUMB:
case MARM_INTERWORK:
case MMCORE_BE:
case MMCORE_LE:
case MMCORE_ELF:
case MMCORE_ELF_LE:
case MARM_EPOC:
return ".rva\t";
default:
/* xgettext:c-format */
fatal (_("Internal error: Unknown machine type: %d"), machine);
break;
}
return "";
}
static const char *
asm_prefix (machine)
int machine;
{
switch (machine)
{
case MARM:
case MPPC:
case MTHUMB:
case MARM_INTERWORK:
case MMCORE_BE:
case MMCORE_LE:
case MMCORE_ELF:
case MMCORE_ELF_LE:
case MARM_EPOC:
break;
case M386:
return "_";
default:
/* xgettext:c-format */
fatal (_("Internal error: Unknown machine type: %d"), machine);
break;
}
return "";
}
#define ASM_BYTE mtable[machine].how_byte
#define ASM_SHORT mtable[machine].how_short
#define ASM_LONG mtable[machine].how_long
#define ASM_TEXT mtable[machine].how_asciz
#define ASM_C mtable[machine].how_comment
#define ASM_JUMP mtable[machine].how_jump
#define ASM_GLOBAL mtable[machine].how_global
#define ASM_SPACE mtable[machine].how_space
#define ASM_ALIGN_SHORT mtable[machine].how_align_short
#define ASM_RVA_BEFORE rvabefore(machine)
#define ASM_RVA_AFTER rvaafter(machine)
#define ASM_PREFIX asm_prefix(machine)
#define ASM_ALIGN_LONG mtable[machine].how_align_long
#define HOW_BFD_READ_TARGET 0 /* always default*/
#define HOW_BFD_WRITE_TARGET mtable[machine].how_bfd_target
#define HOW_BFD_ARCH mtable[machine].how_bfd_arch
#define HOW_JTAB mtable[machine].how_jtab
#define HOW_JTAB_SIZE mtable[machine].how_jtab_size
#define HOW_JTAB_ROFF mtable[machine].how_jtab_roff
#define ASM_SWITCHES mtable[machine].how_default_as_switches
static char **oav;
static void
process_def_file (name)
const char *name;
{
FILE *f = fopen (name, FOPEN_RT);
if (!f)
/* xgettext:c-format */
fatal (_("Can't open def file: %s"), name);
yyin = f;
/* xgettext:c-format */
inform (_("Processing def file: %s"), name);
yyparse ();
inform (_("Processed def file"));
}
/**********************************************************************/
/* Communications with the parser. */
static const char *d_name; /* Arg to NAME or LIBRARY. */
static int d_nfuncs; /* Number of functions exported. */
static int d_named_nfuncs; /* Number of named functions exported. */
static int d_low_ord; /* Lowest ordinal index. */
static int d_high_ord; /* Highest ordinal index. */
static export_type *d_exports; /* List of exported functions. */
static export_type **d_exports_lexically; /* Vector of exported functions in alpha order. */
static dlist_type *d_list; /* Descriptions. */
static dlist_type *a_list; /* Stuff to go in directives. */
static int d_nforwards = 0; /* Number of forwarded exports. */
static int d_is_dll;
static int d_is_exe;
int
yyerror (err)
const char * err ATTRIBUTE_UNUSED;
{
/* xgettext:c-format */
non_fatal (_("Syntax error in def file %s:%d"), def_file, linenumber);
return 0;
}
void
def_exports (name, internal_name, ordinal, noname, constant, data)
const char *name;
const char *internal_name;
int ordinal;
int noname;
int constant;
int data;
{
struct export *p = (struct export *) xmalloc (sizeof (*p));
p->name = name;
p->internal_name = internal_name ? internal_name : name;
p->ordinal = ordinal;
p->constant = constant;
p->noname = noname;
p->data = data;
p->next = d_exports;
d_exports = p;
d_nfuncs++;
if ((internal_name != NULL)
&& (strchr (internal_name, '.') != NULL))
p->forward = ++d_nforwards;
else
p->forward = 0; /* no forward */
}
void
def_name (name, base)
const char *name;
int base;
{
/* xgettext:c-format */
inform (_("NAME: %s base: %x"), name, base);
if (d_is_dll)
non_fatal (_("Can't have LIBRARY and NAME"));
d_name = name;
/* If --dllname not provided, use the one in the DEF file.
FIXME: Is this appropriate for executables? */
if (! dll_name)
dll_name = xstrdup (name);
d_is_exe = 1;
}
void
def_library (name, base)
const char *name;
int base;
{
/* xgettext:c-format */
inform (_("LIBRARY: %s base: %x"), name, base);
if (d_is_exe)
non_fatal (_("Can't have LIBRARY and NAME"));
d_name = name;
/* If --dllname not provided, use the one in the DEF file. */
if (! dll_name)
dll_name = xstrdup (name);
d_is_dll = 1;
}
void
def_description (desc)
const char *desc;
{
dlist_type *d = (dlist_type *) xmalloc (sizeof (dlist_type));
d->text = xstrdup (desc);
d->next = d_list;
d_list = d;
}
static void
new_directive (dir)
char *dir;