/* coff2noff.c
*
* This program reads in a COFF format file, and outputs a NOFF format file.
* The NOFF format is essentially just a simpler version of the COFF file,
* recording where each segment is in the NOFF file, and where it is to
* go in the virtual address space.
*
* Assumes coff file is linked with either
* gld with -N -Ttext 0
* ld with -N -T 0
* to make sure the object file has no shared text.
*
* Also assumes that the COFF file has at most 3 segments:
* .text -- read-only executable instructions
* .data -- initialized data
* .bss/.sbss -- uninitialized data (should be zero'd on program startup)
#ifdef RDATA
* .rdata -- read-only data (e.g., string literals).
* mark this segment readonly to prevent it from being modified
#endif
*
*
* Copyright (c) 1992-1993 The Regents of the University of California.
* All rights reserved. See copyright.h for copyright notice and limitation
* of liability and disclaimer of warranty provisions.
*/
/*
* Modified at UW by KMS, August, 1997
* The modified program always writes the NOFF header in little-endian
* format, rather than host format. This is to avoid the problem
* that user programs run through coff2noff on a big-endian host
* would not run properly on Nachos machines running on little-endian
* hosts.
*
* Note that the Nachos address space loading code
* (in AddrSpace::Load) on big-endian hosts converts the header
* to big-endian format when it is read in.
* Thus, the little-endian header NOFF
* header should work OK whether Nachos is running on a little-endian
* host or a big-endian host.
*/
#define _POSIX_SOURCE 1
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define MAIN
#include "copyright.h"
#undef MAIN
#include "coff.h"
#include "noff.h"
#include <stdarg.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
/****************************************************************/
/* Routines for converting words and short words to and from the
* simulated machine's format of little endian. These end up
* being NOPs when the host machine is little endian.
*/
unsigned int
WordToHost(unsigned int word) {
#ifdef HOST_IS_BIG_ENDIAN
register unsigned long result;
result = (word >> 24) & 0x000000ff;
result |= (word >> 8) & 0x0000ff00;
result |= (word << 8) & 0x00ff0000;
result |= (word << 24) & 0xff000000;
return result;
#else
return word;
#endif /* HOST_IS_BIG_ENDIAN */
}
unsigned short
ShortToHost(unsigned short shortword) {
#if HOST_IS_BIG_ENDIAN
register unsigned short result;
result = (shortword << 8) & 0xff00;
result |= (shortword >> 8) & 0x00ff;
return result;
#else
return shortword;
#endif /* HOST_IS_BIG_ENDIAN */
}
unsigned int
WordToMachine(unsigned int word) { return WordToHost(word); }
unsigned short
ShortToMachine(unsigned short shortword) { return ShortToHost(shortword); }
// this routine was borrowed from userprog/addrspace.cc
// on a big-endian machine, it converts all fields of
// the NOFF header to little-endian format
// on a little-endian machine, where the header is already
// in little-endian format, it does nothing
static void
SwapHeader (NoffHeader *noffH)
{
noffH->noffMagic = WordToHost(noffH->noffMagic);
noffH->code.size = WordToHost(noffH->code.size);
noffH->code.virtualAddr = WordToHost(noffH->code.virtualAddr);
noffH->code.inFileAddr = WordToHost(noffH->code.inFileAddr);
#ifdef RDATA
noffH->readonlyData.size = WordToHost(noffH->readonlyData.size);
noffH->readonlyData.virtualAddr =
WordToHost(noffH->readonlyData.virtualAddr);
noffH->readonlyData.inFileAddr =
WordToHost(noffH->readonlyData.inFileAddr);
#endif
noffH->initData.size = WordToHost(noffH->initData.size);
noffH->initData.virtualAddr = WordToHost(noffH->initData.virtualAddr);
noffH->initData.inFileAddr = WordToHost(noffH->initData.inFileAddr);
noffH->uninitData.size = WordToHost(noffH->uninitData.size);
noffH->uninitData.virtualAddr = WordToHost(noffH->uninitData.virtualAddr);
noffH->uninitData.inFileAddr = WordToHost(noffH->uninitData.inFileAddr);
}
/****************************************************************/
#define ReadStruct(f,s) Read(f,(char *)&s,sizeof(s))
extern char *malloc();
char *noffFileName = NULL;
/* read and check for error */
void Read(int fd, char *buf, int nBytes)
{
if (read(fd, buf, nBytes) != nBytes) {
fprintf(stderr, "File is too short\n");
unlink(noffFileName);
exit(1);
}
}
/* write and check for error */
void Write(int fd, char *buf, int nBytes)
{
if (write(fd, buf, nBytes) != nBytes) {
fprintf(stderr, "Unable to write file\n");
unlink(noffFileName);
exit(1);
}
}
main (int argc, char **argv)
{
int fdIn, fdOut, numsections, i, inNoffFile;
struct filehdr fileh;
struct aouthdr systemh;
struct scnhdr *sections;
char *buffer;
NoffHeader noffH;
if (argc < 2) {
fprintf(stderr, "Usage: %s <coffFileName> <noffFileName>\n", argv[0]);
exit(1);
}
/* open the COFF file (input) */
fdIn = open(argv[1], O_RDONLY, 0);
if (fdIn == -1) {
perror(argv[1]);
exit(1);
}
/* open the NOFF file (output) */
fdOut = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC , 0666);
if (fdIn == -1) {
perror(argv[2]);
exit(1);
}
noffFileName = argv[2];
/* Read in the file header and check the magic number. */
ReadStruct(fdIn,fileh);
fileh.f_magic = ShortToHost(fileh.f_magic);
fileh.f_nscns = ShortToHost(fileh.f_nscns);
if (fileh.f_magic != MIPSELMAGIC) {
fprintf(stderr, "File is not a MIPSEL COFF file\n");
unlink(noffFileName);
exit(1);
}
/* Read in the system header and check the magic number */
ReadStruct(fdIn,systemh);
systemh.magic = ShortToHost(systemh.magic);
if (systemh.magic != OMAGIC) {
fprintf(stderr, "File is not a OMAGIC file\n");
unlink(noffFileName);
exit(1);
}
/* Read in the section headers. */
numsections = fileh.f_nscns;
printf("numsections %d \n",numsections);
sections = (struct scnhdr *)malloc(numsections * sizeof(struct scnhdr));
Read(fdIn, (char *) sections, numsections * sizeof(struct scnhdr));
for (i = 0; i < numsections; i++) {
sections[i].s_paddr = WordToHost(sections[i].s_paddr);
sections[i].s_size = WordToHost(sections[i].s_size);
sections[i].s_scnptr = WordToHost(sections[i].s_scnptr);
}
/* initialize the NOFF header, in case not all the segments are defined
* in the COFF file
*/
noffH.noffMagic = NOFFMAGIC;
noffH.code.size = 0;
noffH.initData.size = 0;
noffH.uninitData.size = 0;
#ifdef RDATA
noffH.readonlyData.size = 0;
#endif
/* Copy the segments in */
inNoffFile = sizeof(NoffHeader);
lseek(fdOut, inNoffFile, 0);
printf("Loading %d sections:\n", numsections);
for (i = 0; i < numsections; i++) {
printf("\t\"%s\", filepos 0x%x, mempos 0x%x, size 0x%x\n",
sections[i].s_name, sections[i].s_scnptr,
sections[i].s_paddr, sections[i].s_size);
if (sections[i].s_size == 0) {
/* do nothing! */
} else if (!strcmp(sections[i].s_name, ".text")) {
noffH.code.virtualAddr = sections[i].s_paddr;
noffH.code.inFileAddr = inNoffFile;
noffH.code.size = sections[i].s_size;
lseek(fdIn, sections[i].s_scnptr, 0);
buffer = malloc(sections[i].s_size);
Read(fdIn, buffer, sections[i].s_size);
Write(fdOut, buffer, sections[i].s_size);
free(buffer);
inNoffFile += sections[i].s_size;
} else if (!strcmp(sections[i].s_name, ".data")){
noffH.initData.virtualAddr = sections[i].s_paddr;
noffH.initData.inFileAddr = inNoffFile;
noffH.initData.size = sections[i].s_size;
lseek(fdIn, sections[i].s_scnptr, 0);
buffer = malloc(sections[i].s_size);
Read(fdIn, buffer, sections[i].s_size);
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共120个文件
h:51个
cc:36个
c:9个
在本科的操作系统教学中,能够提供一个展示真实操作系统是如何工作的工程环境是很重要的,但同时也要求这个工程环境便于学生的理解和修改,所以我们采用Nachos作为操作系统课程的教学实践平台。Nachos是美国加州大学伯克莱分校在操作系统课程中已多次使用的操作系统课程设计平台,在美国很多大学中得到了应用,它在操作系统教学方面具有以下几个突出的优点: 。。。。。。。。见百度百科
资源推荐
资源详情
资源评论
收起资源包目录
NachOs-4.1_110 操作系统实验 (120个子文件)
configure.ac 767B
configure.ac 476B
coff2noff.c 9KB
nachos_syscall.c 7KB
segments.c 1KB
sort.c 1KB
matmult.c 824B
shell.c 682B
halt.c 541B
shell.c 451B
add.c 285B
mipssim.cc 22KB
sysdep.cc 16KB
thread.cc 14KB
filesys.cc 12KB
interrupt.cc 11KB
post.cc 11KB
addrspace.cc 10KB
hash.cc 10KB
list.cc 10KB
synch.cc 9KB
main.cc 9KB
disk.cc 9KB
kernel.cc 8KB
translate.cc 8KB
openfile.cc 7KB
machine.cc 6KB
inheritstack.cc 6KB
directory.cc 6KB
network.cc 6KB
scheduler.cc 6KB
console.cc 5KB
filehdr.cc 5KB
bitmap.cc 5KB
templatestack.cc 4KB
synchlist.cc 4KB
synchconsole.cc 4KB
list.cc 4KB
stack.cc 3KB
exception.cc 3KB
synchdisk.cc 3KB
timer.cc 3KB
pbitmap.cc 3KB
libtest.cc 3KB
alarm.cc 2KB
stats.cc 2KB
debug.cc 1KB
configure 106KB
configure 93KB
COPYRIGHT 1020B
Makefile.dep 31KB
Makefile.dep 2KB
Makefile.dep 647B
Makefile.dep 634B
tut.h 19KB
tut_restartable.h 8KB
machine.h 8KB
mipssim.h 7KB
switch.h 7KB
syscall.h 6KB
post.h 5KB
synch.h 5KB
thread.h 5KB
interrupt.h 5KB
nachos_syscall.h 5KB
list.h 5KB
errno.h 4KB
network.h 4KB
tut_reporter.h 4KB
hash.h 4KB
disk.h 4KB
debug.h 4KB
console.h 3KB
filesys.h 3KB
openfile.h 3KB
inheritstack.h 3KB
directory.h 3KB
sysdep.h 3KB
stats.h 2KB
filehdr.h 2KB
coff.h 2KB
kernel.h 2KB
bitmap.h 2KB
synchdisk.h 2KB
timer.h 2KB
translate.h 2KB
addrspace.h 2KB
synchconsole.h 2KB
synchlist.h 2KB
scheduler.h 1KB
callback.h 1KB
stack.h 1KB
alarm.h 1KB
templatestack.h 1KB
copyright.h 1KB
copyright.h 1KB
copyright.h 1KB
pbitmap.h 1KB
utility.h 1KB
list.h 919B
共 120 条
- 1
- 2
资源评论
- zhuoan20102012-12-25很好,和我们的实验一样
- weishuaixiaohong2014-05-10要求有点差别,不过聊胜于无吧~
- lqc_solo2013-05-17跟我们的实验要求有点差别,不过聊胜于无吧~
hbhdytf
- 粉丝: 0
- 资源: 32
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- delphi实现DBGrid全选和反选功能
- 25C11F41-2B2A-4D1A-AAA8-7C654526B129.pdf
- Android Studio Jellyfish(android-studio-2023.3.1.18-cros.deb)
- MVC+EF框架+EasyUI实现权限管理源码程序
- python第66-75天,Day66-75.rar
- python后端服务project-of-tornado.rar
- python测验,hello-tornado.rar
- 基于SpringBoot+Vue3快速开发平台、自研工作流引擎源码设计.zip
- docker安装部署全流程
- 基于树莓派的人脸识别系统python源码+项目部署说明+超详细代码注释.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功