Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
targ_em_dwarf.cxx
Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001 Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2 of the GNU General Public License as
00007   published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if 
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU General Public License along
00021   with this program; if not, write the Free Software Foundation, Inc., 59
00022   Temple Place - Suite 330, Boston MA 02111-1307, USA.
00023 
00024   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00025   Mountain View, CA 94043, or:
00026 
00027   http://www.sgi.com
00028 
00029   For further information regarding this notice, see:
00030 
00031   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00032 
00033 */
00034 
00035 
00036 #include <stdlib.h>
00037 #include <stdio.h>
00038 #include <elf.h>
00039 #include <elfaccess.h>
00040 #include <libelf.h>
00041 #include <libdwarf.h>
00042 #include "targ_em_dwarf.h"
00043 #include <assert.h>     // temporary
00044 #define USE_STANDARD_TYPES 1
00045 #include "defs.h"
00046 
00047 struct UINT32_unaligned {
00048   UINT32 val;
00049 } __attribute__ ((aligned (1)));
00050 
00051 struct UINT64_unaligned {
00052   UINT64 val;
00053 } __attribute__ ((aligned (1)));
00054 
00055 static Elf32_Rel *
00056 translate_reloc32(Dwarf_Relocation_Data       rentry,
00057                   Cg_Dwarf_Sym_To_Elfsym_Ofst translate_symndx,
00058                   Dwarf_Ptr                   buffer,
00059                   Dwarf_Unsigned              bufsize)
00060 {
00061   static Elf32_Rel retval;
00062   Dwarf_Unsigned   elf_symidx, elf_symoff;
00063 
00064   REL32_offset(retval) = rentry->drd_offset;
00065 
00066   translate_symndx(rentry->drd_symbol_index, &elf_symidx, &elf_symoff);
00067   Set_REL32_sym(retval, elf_symidx);
00068 
00069   assert(rentry->drd_offset <= bufsize - sizeof(long));
00070 
00071   ((UINT32_unaligned *) ((char *) buffer + rentry->drd_offset))->val +=
00072       (UINT32) elf_symoff;
00073 
00074   switch (rentry->drd_type) {
00075   case dwarf_drt_data_reloc:
00076     Set_REL32_type(retval, R_IA_64_DIR32LSB);
00077     break;
00078   case dwarf_drt_segment_rel:
00079     Set_REL32_type(retval, R_IA_64_SEGREL32LSB);
00080     break;
00081   default:
00082     fprintf(stderr, "ERROR: Unrecognized symbolic relocation type\n");
00083     exit(-1);
00084   }
00085   return &retval;
00086 }
00087 
00088 static Elf64_Rel *
00089 translate_reloc64(Dwarf_Relocation_Data       rentry,
00090                   Cg_Dwarf_Sym_To_Elfsym_Ofst translate_symndx,
00091                   Dwarf_Ptr                   buffer,
00092                   Dwarf_Unsigned              bufsize)
00093 {
00094   static Elf64_Rel retval;
00095   Dwarf_Unsigned   elf_symidx, elf_symoff;
00096 
00097   REL_offset(retval) = rentry->drd_offset;
00098 
00099   translate_symndx(rentry->drd_symbol_index, &elf_symidx, &elf_symoff);
00100   Set_REL64_sym(retval, elf_symidx);
00101 
00102   assert(rentry->drd_offset <= bufsize - sizeof(long long));
00103 
00104   ((UINT64_unaligned *) ((char *) buffer + rentry->drd_offset))->val +=
00105     (UINT64) elf_symoff;
00106 
00107   switch (rentry->drd_type) {
00108   case dwarf_drt_data_reloc:
00109     if(rentry->drd_length == 8) {
00110       Set_REL64_type(retval, R_IA_64_DIR64LSB);
00111     } else {
00112       // if not 8, better be 4
00113       Set_REL64_type(retval, R_IA_64_DIR32LSB);
00114     }
00115     break;
00116   case dwarf_drt_segment_rel:
00117     Set_REL64_type(retval, R_IA_64_SEGREL64LSB);
00118     break;
00119   default:
00120     fprintf(stderr, "ERROR: Unrecognized symbolic relocation type\n");
00121     exit(-1);
00122   }
00123   return &retval;
00124 }
00125 
00126 Dwarf_Ptr
00127 Em_Dwarf_Symbolic_Relocs_To_Elf(next_buffer_retriever     get_buffer,
00128                                 next_bufsize_retriever    get_bufsize,
00129                                 advancer_to_next_stream   advance_stream,
00130                                 Dwarf_Signed              buffer_scndx,
00131                                 Dwarf_Relocation_Data     reloc_buf,
00132                                 Dwarf_Unsigned            entry_count,
00133                                 int                       is_64bit,
00134                                 Cg_Dwarf_Sym_To_Elfsym_Ofst translate_symndx,
00135                                 Dwarf_Unsigned           *result_buf_size)
00136 {
00137   unsigned i;
00138   unsigned step_size = (is_64bit ? sizeof(Elf64_Rel) : sizeof(Elf32_Rel));
00139 
00140   Dwarf_Ptr  result_buf = (Dwarf_Ptr *) malloc(step_size * entry_count);
00141   char *cur_reloc = (char *) result_buf;
00142 
00143   Dwarf_Unsigned offset_offset = 0;
00144   Dwarf_Unsigned bufsize = 0;
00145   Dwarf_Ptr      buffer = NULL;
00146 
00147   for (i = 0; i < entry_count; ++i) {
00148     if (reloc_buf->drd_type == dwarf_drt_none) {
00149     }
00150     else if (reloc_buf->drd_type == dwarf_drt_first_of_length_pair) {
00151       if ((i == (entry_count - 1)) ||
00152           reloc_buf->drd_offset != (reloc_buf+1)->drd_offset ||
00153           (reloc_buf+1)->drd_type != dwarf_drt_second_of_length_pair) {
00154         fprintf(stderr, "ERROR: Malformed symbol-difference pair");
00155         exit(-1);
00156       }
00157       else {
00158 #if 0
00159         fprintf(TFile, "translating pair (0x%lx) aimed at offset %llu\n",
00160                 reloc_buf, reloc_buf->drd_offset);
00161 #endif
00162 
00163         Dwarf_Unsigned offset1, offset2;
00164         Dwarf_Unsigned scn_sym1, scn_sym2;
00165         translate_symndx(reloc_buf->drd_symbol_index,
00166                          &scn_sym1,
00167                          &offset1);
00168         ++reloc_buf;
00169         ++i;
00170         translate_symndx(reloc_buf->drd_symbol_index,
00171                          &scn_sym2,
00172                          &offset2);
00173         if (scn_sym1 != scn_sym2) {
00174           fprintf(stderr,
00175                   "ERROR: Cannot compute label difference "
00176                   "across sections");
00177           exit(-1);
00178         }
00179         while (reloc_buf->drd_offset >= bufsize + offset_offset) {
00180           offset_offset += bufsize;
00181           if (!advance_stream(buffer_scndx)) {
00182             assert(FALSE);
00183           }
00184           buffer = get_buffer();
00185           bufsize = get_bufsize();
00186 #if 0
00187           fprintf(TFile, "buffer = 0x%lx, bufsize = %llu\n", buffer, bufsize);
00188 #endif
00189         }
00190         if (is_64bit) {
00191           ((UINT64_unaligned *) ((char *) buffer +
00192                                  reloc_buf->drd_offset - offset_offset))->val +=
00193             offset2 - offset1;
00194         }
00195         else {
00196           ((UINT32_unaligned *) ((char *) buffer +
00197                                  reloc_buf->drd_offset - offset_offset))->val +=
00198             offset2 - offset1;
00199         }
00200       }
00201     }
00202     else {
00203 #if 0
00204         fprintf(TFile, "translating simple (0x%lx) aimed at offset %llu\n",
00205                 reloc_buf, reloc_buf->drd_offset);
00206 #endif
00207 
00208         while (reloc_buf->drd_offset >= bufsize + offset_offset) {
00209           offset_offset += bufsize;
00210           if (!advance_stream(buffer_scndx)) {
00211             assert(FALSE);
00212           }
00213           buffer = get_buffer();
00214           bufsize = get_bufsize();
00215 #if 0
00216           fprintf(TFile, "buffer = 0x%lx, bufsize = %llu\n", buffer, bufsize);
00217 #endif
00218         }
00219       if (is_64bit) {
00220         *((Elf64_Rel *) cur_reloc) =
00221           *translate_reloc64(reloc_buf,
00222                              translate_symndx,
00223                              // DON'T TRY THIS AT HOME
00224                              (Dwarf_Ptr) ((char *) buffer - offset_offset),
00225                              bufsize + offset_offset);
00226       }
00227       else {
00228         *((Elf32_Rel *) cur_reloc) =
00229           *translate_reloc32(reloc_buf,
00230                              translate_symndx,
00231                              // DON'T TRY THIS AT HOME
00232                              (Dwarf_Ptr) ((char *) buffer - offset_offset),
00233                              bufsize + offset_offset);
00234       }
00235       cur_reloc += step_size;
00236     }
00237     ++reloc_buf;
00238   }
00239   *result_buf_size = cur_reloc - (char *) result_buf;
00240   return (Dwarf_Ptr) result_buf;
00241 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines