Example #1
0
char *build_module(struct manifest *m, bool keep, char **errstr)
{
	char *name = link_objects(m, m->basename, false, obj_list(m), errstr);
	if (name) {
		if (keep) {
			char *realname = talloc_asprintf(m, "%s.o", m->dir);
			/* We leave this object file around, all built. */
			if (!move_file(name, realname))
				err(1, "Renaming %s to %s", name, realname);
			name = realname;
		}
	}
	return name;
}
Example #2
0
int main(int argc, char** argv){
    int c;
    setenv("POSIXLY_CORRECT", "1", 1);
    opterr = 0;
    object_files = g_array_new(FALSE,FALSE, sizeof(object_file_entry));
    FILE* output = NULL;
    while(optind < argc){
        if((c=getopt(argc,argv,"o:"))!= -1){
            switch(c){
                case 'o':
                    output = fopen(optarg,"wb");
                    break;

            }
        }else{ // non option argument
            FILE* input = fopen(argv[optind],"rb");
            if(input == NULL){
                fprintf(stderr, "Could not open file: %s\n",argv[optind]);
                exit(1);
            }
            aout_header header;
            object_file_entry entry;
            memset(&entry,0, sizeof(object_file_entry));
            fread(&header, 1, sizeof(aout_header),input);
            if(header.a_magic != A_MAGIC){
                fprintf(stderr, "File \"%s\"is not in d16 a.out format. Perhaps you provided a binary?\nMagic Number: 0x%x",argv[optind],header.a_magic);
                exit(1);
            }
            entry.header = header;
            if(header.a_text > 0) {
                entry.text = malloc(header.a_text);
                //fseek(input, sizeof(header), SEEK_SET);
                fread(entry.text, header.a_text, 1, input);

            }
            if(header.a_data >0){
                entry.data = malloc(header.a_data);
                fread(entry.data, header.a_data, 1, input);
            }
            if(header.a_syms >0){
                entry.syms = malloc(header.a_syms);
                fread(entry.syms, header.a_syms,1,input);
            }
            if(header.a_trsize > 0){
                entry.text_relocs = malloc(header.a_trsize);
                fread(entry.text_relocs,header.a_trsize,1,input);
            }
            if(header.a_drsize >0 ){
                entry.data_relocs = malloc(header.a_drsize);
                fread(entry.data_relocs,header.a_drsize,1,input);
            }
            long pos = ftell(input);
            fseek(input,0,SEEK_END);
            long len = ftell(input)-pos;
            entry.strings = malloc(len);
            fseek(input,pos,SEEK_SET);
            fread(entry.strings,len,1,input);
            fclose(input);
            g_array_append_val(object_files,entry);


            optind++;
        }
    }
    if(output == NULL){
        fprintf(stderr,"d16-ld: No output file specified\n");
        exit(-1);
    }

    link_objects(object_files,output);


}
Example #3
0
File: main.c Project: KnightOS/scas
int main(int argc, char **argv) {
	init_scas_runtime();
	parse_arguments(argc, argv);
	init_log(scas_runtime.verbosity);
	validate_scas_runtime();
	instruction_set_t *instruction_set = find_inst();
	scas_log(L_INFO, "Loaded instruction set: %s", instruction_set->arch);
	list_t *include_path = split_include_path();
	list_t *errors = create_list();
	list_t *warnings = create_list();

	list_t *objects = create_list();
	int i;
	for (i = 0; i < scas_runtime.input_files->length; ++i) {
		scas_log(L_INFO, "Assembling input file: '%s'", scas_runtime.input_files->items[i]);
		indent_log();
		FILE *f;
		if (strcasecmp(scas_runtime.input_files->items[i], "-") == 0) {
			f = stdin;
		} else {
			f = fopen(scas_runtime.input_files->items[i], "r");
		}
		if (!f) {
			scas_abort("Unable to open '%s' for assembly.", scas_runtime.input_files->items[i]);
		}
		char magic[7];
		bool is_object = false;
		if (fread(magic, sizeof(char), 7, f) == 7) {
			if (strncmp("SCASOBJ", magic, 7) == 0) {
				is_object = true;
			}
		}
		fseek(f, 0L, SEEK_SET);

		object_t *o;
		if (is_object) {
			scas_log(L_INFO, "Loading object file '%s'", scas_runtime.input_files->items[i]);
			o = freadobj(f, scas_runtime.input_files->items[i]);
		} else {
			assembler_settings_t settings = {
				.include_path = include_path,
				.set = instruction_set,
				.errors = errors,
				.warnings = warnings,
				.macros = scas_runtime.macros,
			};
			o = assemble(f, scas_runtime.input_files->items[i], &settings);
			fclose(f);
			scas_log(L_INFO, "Assembler returned %d errors, %d warnings for '%s'",
					errors->length, warnings->length, scas_runtime.input_files->items[i]);
		}
		list_add(objects, o);
		deindent_log();
	}

	scas_log(L_DEBUG, "Opening output file for writing: %s", scas_runtime.output_file);
	FILE *out;
	if (strcasecmp(scas_runtime.output_file, "-") == 0) {
		out = stdout;
	} else {
		out = fopen(scas_runtime.output_file, "w+");
	}
	if (!out) {
		scas_abort("Unable to open '%s' for output.", scas_runtime.output_file);
	}

	if ((scas_runtime.jobs & LINK) == LINK) {
		scas_log(L_INFO, "Passing objects to linker");
		linker_settings_t settings = {
			.automatic_relocation = scas_runtime.options.auto_relocation,
			.merge_only = (scas_runtime.jobs & MERGE) == MERGE,
			.errors = errors,
			.warnings = warnings,
			.write_output = scas_runtime.options.output_format
		};
		if (settings.merge_only) {
			object_t *merged = merge_objects(objects);
			fwriteobj(out, merged);
		} else {
			link_objects(out, objects, &settings);
		}
		scas_log(L_INFO, "Linker returned %d errors, %d warnings", errors->length, warnings->length);
	} else {
		scas_log(L_INFO, "Skipping linking - writing to object file");
		object_t *merged = merge_objects(objects);
		fwriteobj(out, merged);
		fflush(out);
		fclose(out);
	}
	if (errors->length != 0) {
		int i;
		for (i = 0; i < errors->length; ++i) {
			error_t *error = errors->items[i];
			fprintf(stderr, "%s:%d:%d: error #%d: %s\n", error->file_name,
					(int)error->line_number, (int)error->column, error->code,
					error->message);
			fprintf(stderr, "%s\n", error->line);
			if (error->column != 0) {
				int j;
				for (j = error->column; j > 0; --j) {
					fprintf(stderr, ".");
				}
				fprintf(stderr, "^\n");
			} else {
				fprintf(stderr, "\n");
			}
		}
		remove(scas_runtime.output_file);
	}
	if (warnings->length != 0) {
		int i;
		for (i = 0; i < errors->length; ++i) {
			warning_t *warning = warnings->items[i];
			fprintf(stderr, "%s:%d:%d: warning #%d: %s\n", warning->file_name,
					(int)warning->line_number, (int)warning->column, warning->code,
					get_warning_string(warning));
			fprintf(stderr, "%s\n", warning->line);
			if (warning->column != 0) {
				int j;
				for (j = warning->column; j > 0; --j) {
					fprintf(stderr, ".");
				}
				fprintf(stderr, "^\n");
			}
		}
	}

	int ret = errors->length;
	scas_log(L_DEBUG, "Exiting with status code %d, cleaning up", ret);
	list_free(scas_runtime.input_files);
	free_flat_list(include_path);
	list_free(objects);
	list_free(errors);
	list_free(warnings);
	instruction_set_free(instruction_set);
	return ret;
}