@@ -110,6 +110,46 @@ std::optional<std::string> getPrefix(StringRef Argv0) {
110
110
return ProgName.str ();
111
111
}
112
112
113
+ bool parseModuleDefinition (StringRef DefFileName, MachineTypes Machine,
114
+ bool AddUnderscores,
115
+ std::vector<COFFShortExport> &Exports,
116
+ std::string &OutputFile) {
117
+ std::unique_ptr<MemoryBuffer> MB = openFile (DefFileName);
118
+ if (!MB)
119
+ return false ;
120
+
121
+ if (!MB->getBufferSize ()) {
122
+ llvm::errs () << " definition file empty\n " ;
123
+ return false ;
124
+ }
125
+
126
+ Expected<COFFModuleDefinition> Def = parseCOFFModuleDefinition (
127
+ *MB, Machine, /* MingwDef=*/ true , AddUnderscores);
128
+ if (!Def) {
129
+ llvm::errs () << " error parsing definition\n "
130
+ << errorToErrorCode (Def.takeError ()).message () << " \n " ;
131
+ return false ;
132
+ }
133
+
134
+ if (OutputFile.empty ())
135
+ OutputFile = std::move (Def->OutputFile );
136
+
137
+ // If ExtName is set (if the "ExtName = Name" syntax was used), overwrite
138
+ // Name with ExtName and clear ExtName. When only creating an import
139
+ // library and not linking, the internal name is irrelevant. This avoids
140
+ // cases where writeImportLibrary tries to transplant decoration from
141
+ // symbol decoration onto ExtName.
142
+ for (COFFShortExport &E : Def->Exports ) {
143
+ if (!E.ExtName .empty ()) {
144
+ E.Name = E.ExtName ;
145
+ E.ExtName .clear ();
146
+ }
147
+ }
148
+
149
+ Exports = std::move (Def->Exports );
150
+ return true ;
151
+ }
152
+
113
153
} // namespace
114
154
115
155
int llvm::dlltoolDriverMain (llvm::ArrayRef<const char *> ArgsArr) {
@@ -141,16 +181,6 @@ int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
141
181
return 1 ;
142
182
}
143
183
144
- std::unique_ptr<MemoryBuffer> MB =
145
- openFile (Args.getLastArg (OPT_d)->getValue ());
146
- if (!MB)
147
- return 1 ;
148
-
149
- if (!MB->getBufferSize ()) {
150
- llvm::errs () << " definition file empty\n " ;
151
- return 1 ;
152
- }
153
-
154
184
COFF::MachineTypes Machine = getDefaultMachine ();
155
185
if (std::optional<std::string> Prefix = getPrefix (ArgsArr[0 ])) {
156
186
Triple T (*Prefix);
@@ -166,40 +196,23 @@ int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
166
196
}
167
197
168
198
bool AddUnderscores = !Args.hasArg (OPT_no_leading_underscore);
169
- Expected<COFFModuleDefinition> Def = parseCOFFModuleDefinition (
170
- *MB, Machine, /* MingwDef=*/ true , AddUnderscores);
171
199
172
- if (!Def) {
173
- llvm::errs () << " error parsing definition\n "
174
- << errorToErrorCode (Def.takeError ()).message () << " \n " ;
175
- return 1 ;
176
- }
177
-
178
- // Do this after the parser because parseCOFFModuleDefinition sets OutputFile.
200
+ std::string OutputFile;
179
201
if (auto *Arg = Args.getLastArg (OPT_D))
180
- Def-> OutputFile = Arg->getValue ();
202
+ OutputFile = Arg->getValue ();
181
203
182
- if (Def->OutputFile .empty ()) {
183
- llvm::errs () << " no DLL name specified\n " ;
204
+ std::vector<COFFShortExport> Exports;
205
+ if (!parseModuleDefinition (Args.getLastArg (OPT_d)->getValue (), Machine,
206
+ AddUnderscores, Exports, OutputFile))
184
207
return 1 ;
185
- }
186
208
187
- std::string Path = std::string (Args.getLastArgValue (OPT_l));
188
-
189
- // If ExtName is set (if the "ExtName = Name" syntax was used), overwrite
190
- // Name with ExtName and clear ExtName. When only creating an import
191
- // library and not linking, the internal name is irrelevant. This avoids
192
- // cases where writeImportLibrary tries to transplant decoration from
193
- // symbol decoration onto ExtName.
194
- for (COFFShortExport& E : Def->Exports ) {
195
- if (!E.ExtName .empty ()) {
196
- E.Name = E.ExtName ;
197
- E.ExtName .clear ();
198
- }
209
+ if (OutputFile.empty ()) {
210
+ llvm::errs () << " no DLL name specified\n " ;
211
+ return 1 ;
199
212
}
200
213
201
214
if (Machine == IMAGE_FILE_MACHINE_I386 && Args.hasArg (OPT_k)) {
202
- for (COFFShortExport& E : Def-> Exports ) {
215
+ for (COFFShortExport & E : Exports) {
203
216
if (!E.AliasTarget .empty () || (!E.Name .empty () && E.Name [0 ] == ' ?' ))
204
217
continue ;
205
218
E.SymbolName = E.Name ;
@@ -215,8 +228,9 @@ int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
215
228
}
216
229
}
217
230
218
- if (!Path.empty () && writeImportLibrary (Def->OutputFile , Path, Def->Exports ,
219
- Machine, /* MinGW=*/ true ))
231
+ std::string Path = std::string (Args.getLastArgValue (OPT_l));
232
+ if (!Path.empty () && writeImportLibrary (OutputFile, Path, Exports, Machine,
233
+ /* MinGW=*/ true ))
220
234
return 1 ;
221
235
return 0 ;
222
236
}
0 commit comments