/
main.cpp
121 lines (114 loc) · 2.25 KB
/
main.cpp
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
/*
* main.cpp
*
* Created on: Aug 4, 2013
* Author: ns
*/
#include <iostream>
#include <vector>
#include <string>
#include <unistd.h>
#include "head/defines.h"
#include "head/builderClass.h"
int main( int argc,char *argv[] )
{
const char *optStr = ":o:l:";
vector<string> objPath;
string libPath;
string outputPath;
int opt = 0;
while((opt=getopt(argc,argv,optStr)) != -1 )
{
switch( opt )
{
case 'o':
if( outputPath.length() )
{
cout << "错误:定义了多个输出路径" << endl;
return 0;
}
outputPath = optarg;
break;
case 'l':
if( libPath.size() )
{
cout << "错误:定义了多个库路径" << endl;
return 0;
}
libPath = optarg;
break;
case ':':
cout << "缺少参数" << endl;
break;
case '?':
cout << "忽略错误选项:" << (char)optopt << endl;
break;
}
}
if( !libPath.size() )
{
cout << "错误:缺少库" << endl;
return 0;
}
if( !outputPath.size() )
{
cout << "错误:没有定义输出路径" << endl;
return 0;
}
for( ;optind < argc;++optind )
objPath.push_back(argv[optind]);
if( objPath.size() == 0 )
{
cout << "错误:缺少输入" << endl;
return 0;
}
Builder bd( objPath,libPath,outputPath);
if( !bd.CollectSymbol() )
{
cout << "失败退出" << endl;
return 0;
}
bd.DoMerge();
bd.GetSymbolAddress();
bd.Relocation();
if( !bd.GenerateBinary() )
{
cout << "失败退出" << endl;
return 0;
}
return 0;
}
//#include <iostream>
//#include <vector>
//#include <string>
//#include <elf.h>
//#include "head/defines.h"
//#include "head/objectFileClass.h"
//#include "head/sectionClass.h"
//#include "head/builderClass.h"
//
//using namespace std;
//
//string g_spFileName = "/home/ns/linkerLab/simple/a.o";
//string g_spFileName2 = "/home/ns/linkerLab/simple/b.o";
//string g_output = "/home/ns/linkerLab/simple/my_ab";
////string g_lib = "/home/ns/linkerLab/minicrt";
//string g_lib = "/home/ns/linkerLab/libc-o";
//
//int main()
//{
// vector<string> objPath;
//
// objPath.push_back( g_spFileName );
//// objPath.push_back( g_spFileName2 );
// Builder bd( objPath,g_lib,g_output );
//
// if( !bd.CollectSymbol() )
// return 0;
// bd.DoMerge();
// bd.GetSymbolAddress();
// bd.Relocation();
// bd.GenerateBinary();
//
// return 0;
//}