Changeset 14590


Ignore:
Timestamp:
Jan 21, 2011, 10:25:33 PM (14 years ago)
Author:
Gary Byers
Message:

lisp-debug.c: in debug_thread_info(), don't coerce everything to u64_t.
mach_o_image.c: more work-in-progress code.

Location:
trunk/source/lisp-kernel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/lisp-kernel/lisp-debug.c

    r14484 r14590  
    886886            (cs_area->low), (cs_area->high));
    887887    fprintf(dbgout, "Value (lisp) stack area: low = 0x" LISP ", high = 0x" LISP "\n",
    888             (u64_t)(natural)(vs_area->low), (u64_t)(natural)vs_area->high);
     888            (natural)(vs_area->low), (natural)vs_area->high);
    889889    if (xp) {
    890890      fprintf(dbgout, "Exception stack pointer = 0x" LISP "\n",
    891891#ifdef PPC
    892               (u64_t) (natural)(xpGPR(xp,1))
     892              (natural)(xpGPR(xp,1))
    893893#endif
    894894#ifdef X86
    895               (u64_t) (natural)(xpGPR(xp,Isp))
     895              (natural)(xpGPR(xp,Isp))
    896896#endif           
    897897#ifdef ARM
    898               (u64_t) (natural)(xpGPR(xp,Rsp))
     898              (natural)(xpGPR(xp,Rsp))
    899899#endif
    900900              );
  • trunk/source/lisp-kernel/mach-o-image.c

    r14584 r14590  
    2626typedef struct mach_header_64 macho_header;
    2727#define MACHO_MAGIC MH_MAGIC_64
     28#define MACHO_LC_SEGMENT LC_SEGMENT_64
    2829typedef struct segment_command_64 macho_segment_command;
    2930typedef struct section_64 macho_section;
     
    3233typedef struct mach_header_64 macho_header;
    3334#define MACHO_MAGIC MH_MAGIC
     35#define MACHO_LC_SEGMENT LC_SEGMENT
    3436typedef struct segment_command macho_segment_command;
    3537typedef struct section macho_section;
     
    5355macho_symbol_table *all_symbols, *local_symbols, *defined_external_symbols, *undefined_external_symbols;
    5456
     57macho_section *
     58nth_section_in_segment(macho_segment_command *segment, int sectno)
     59{
     60  return (macho_section *)(((char *)segment)+sizeof(macho_segment_command)+(sizeof(macho_section) *sectno));
     61}
     62
    5563ssize_t
    5664safe_read(int fd, char *buf, size_t nbytes)
     
    7179    if (n == 0) {
    7280      fprintf(stderr, "unexpected end of file reading image\n");
     81      exit(1);
     82    }
     83    total += n;
     84    buf += n;
     85  }
     86  return total;
     87}
     88
     89ssize_t
     90safe_write(int fd, char *buf, size_t nbytes)
     91{
     92  size_t total = 0;
     93  ssize_t n;
     94  while (total <  nbytes) {
     95    n = nbytes-total;
     96    if (n > INT_MAX) {
     97      n = INT_MAX;
     98    }
     99    n = write(fd, buf, n);
     100    if (n < 0) {
     101      perror("writing to image");
    73102      exit(1);
    74103    }
     
    216245}
    217246
     247void
     248add_lisp_function_stab(LispObj f, natural size_in_bytes, macho_string_table *strings, macho_symbol_table *syms, int section_ordinal)
     249{
     250  macho_nlist symbol;
     251 
     252  symbol.n_type = N_BNSYM;
     253  symbol.n_un.n_strx = 1;
     254  symbol.n_sect = section_ordinal;
     255  symbol.n_desc = 0;
     256  symbol.n_value = f;
     257  add_symbol(&symbol, syms);
     258
     259  symbol.n_type = N_FUN;
     260  symbol.n_un.n_strx = save_string(print_lisp_object(f),strings);
     261  symbol.n_sect = section_ordinal;
     262  symbol.n_desc = 0;
     263  symbol.n_value = f;
     264  add_symbol(&symbol, syms);
     265
     266  symbol.n_type = N_FUN;
     267  symbol.n_un.n_strx = 1;
     268  symbol.n_sect = NO_SECT;
     269  symbol.n_desc = 0;
     270  symbol.n_value = size_in_bytes;
     271  add_symbol(&symbol, syms);
     272
     273  symbol.n_type = N_ENSYM;
     274  symbol.n_un.n_strx = 1;
     275  symbol.n_sect = section_ordinal;
     276  symbol.n_desc = 0;
     277  symbol.n_value = size_in_bytes;
     278  add_symbol(&symbol, syms);
     279}
     280
     281#ifdef X86
     282void
     283add_lisp_function_stabs(macho_symbol_table *symbols, macho_string_table *strings, int section_ordinal)
     284{
     285  LispObj
     286    *start = (LispObj *) readonly_area->low,
     287    *end = (LispObj *) readonly_area->active,
     288    header,
     289    f;
     290  int tag;
     291  natural size_in_bytes;
     292
     293  while (start < end) {
     294    header = *start;
     295    tag = header_subtag(header);
     296    if (tag == subtag_function) {
     297#ifdef X8632
     298      f = ((LispObj)start)+fulltag_misc;
     299      size_in_bytes = (header_element_count(header)<<node_shift)-tag_misc;
     300#endif
     301#ifdef X8664
     302      f = ((LispObj)start)+fulltag_function;
     303      size_in_bytes = (header_element_count(header)<<node_shift)-tag_function;
     304#endif
     305
     306      add_lisp_function_stab(f,size_in_bytes,strings,symbols,section_ordinal);
     307      start += ((header_element_count(header)+2)&~1);
     308    } else {
     309      start = (LispObj *)skip_over_ivector((LispObj)start,header);
     310    }
     311  }
     312}
     313#endif
     314
     315typedef struct {
     316  char page[4096];
     317  int used;
     318  int load_command_offsets[16];
     319} macho_prefix;
     320
     321macho_prefix *
     322init_macho_prefix(uint32_t magic, cpu_type_t cputype, cpu_subtype_t cpusubtype, uint32_t filetype, uint32_t flags) {
     323  macho_prefix *p = calloc(1,sizeof(macho_prefix));
     324  macho_header *h = (macho_header *) p;
     325
     326  h->magic = magic;
     327  h->cputype = cputype;
     328  h->cpusubtype = cpusubtype;
     329  h->filetype = filetype;
     330  h->flags = flags;
     331  p->used = sizeof(macho_header);
     332  return p;
     333}
     334
     335struct load_command *
     336add_load_command(macho_prefix *p, uint32_t cmd, uint32_t cmdsize)
     337{
     338  struct load_command *l = (struct load_command *)&(p->page[p->used]);
     339  macho_header *h = (macho_header *)p;
     340
     341  cmdsize = align_to_power_of_2(cmdsize,node_shift);
     342  p->load_command_offsets[h->ncmds] = p->used;
     343  p->used += cmdsize;
     344  l->cmd = cmd;
     345  l->cmdsize += cmdsize;
     346  h->ncmds++;
     347  h->sizeofcmds += cmdsize;
     348  return l;
     349}
     350
     351macho_segment_command *
     352add_segment(macho_prefix *p,char *segname, int nsections, ...) /* sectnames */
     353{
     354  macho_segment_command *seg = (macho_segment_command *) add_load_command(p, MACHO_LC_SEGMENT, sizeof(macho_segment_command)+(nsections * sizeof(macho_section)));
     355  macho_section *sect = nth_section_in_segment(seg, 0);
     356  va_list sectnames;
     357  char *sectname;
     358 
     359  seg->nsects = nsections;
     360  strncpy(seg->segname,segname,sizeof(seg->segname));
     361  va_start(sectnames,nsections);
     362  while(nsections--) {
     363    sectname = va_arg(sectnames,char *);
     364    strncpy(sect->sectname,sectname,sizeof(sect->sectname));
     365    strncpy(sect->segname,segname,sizeof(sect->segname));
     366    sect++;
     367  }
     368  return seg;
     369}
     370
     371   
     372void
     373save_native_library(int fd)
     374{
     375  macho_prefix *p = init_macho_prefix(MACHO_MAGIC,
     376#ifdef X8632
     377                                      CPU_TYPE_I386,
     378                                      CPU_SUBTYPE_X86_ALL,
     379#endif
     380#ifdef X8664
     381                                      CPU_TYPE_X86_64,
     382                                      CPU_SUBTYPE_X86_64_ALL,
     383#endif
     384#ifdef PPC32
     385                                      CPU_TYPE_POWERPC,
     386                                      CPU_SUBTYPE_POWERPC_ALL,
     387#endif
     388#ifdef PPC64
     389                                      CPU_TYPE_POWERPC64,
     390                                      CPU_TYPE_POWERPC_ALL,
     391#endif
     392#ifdef ARM
     393                                      CPU_TYPE_ARM,
     394                                      CPU_SUBTYPE_ARM_ALL,
     395#endif
     396                                      MH_DYLIB,
     397                                      MH_NOUNDEFS);
     398  macho_segment_command *seg;
     399  macho_section *sect;
     400  off_t curpos = 4096;
     401
     402
     403  seg = add_segment(p, "innocent", 1, "bystander");
     404  seg->vmaddr = (natural)(readonly_area->low-4096);
     405  seg->vmsize = 4096;
     406  seg->fileoff = 0;
     407  seg->filesize = 4096;
     408  seg->maxprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
     409  seg->initprot = VM_PROT_READ|VM_PROT_EXECUTE;
     410  sect = nth_section_in_segment(seg,0);
     411  sect->addr = (natural)(readonly_area->low-1);
     412  sect->size = 1;
     413  sect->offset = 4095;
     414  sect->flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
     415  p->page[4095] = 0xcc;
     416
     417  seg = add_segment(p,"READONLY",1,"readonly");
     418  seg->vmaddr = (natural)(readonly_area->low);
     419  seg->vmsize = align_to_power_of_2(readonly_area->active-readonly_area->low,12);
     420  seg->fileoff = curpos;
     421  seg->filesize = seg->vmsize;
     422  seg->maxprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
     423  seg->initprot = VM_PROT_READ|VM_PROT_EXECUTE;
     424  sect = nth_section_in_segment(seg,0);
     425  sect->addr = (natural)(readonly_area->low);
     426  sect->size = readonly_area->active-readonly_area->low;
     427  sect->offset = seg->fileoff;
     428  sect->flags = S_ATTR_SOME_INSTRUCTIONS;
     429  lseek(fd,curpos,SEEK_SET);
     430  safe_write(fd,readonly_area->low,seg->filesize);
     431
     432
     433
     434}
Note: See TracChangeset for help on using the changeset viewer.