Print this page
5688 ELF tools need to be more careful with dwarf data


  82  * DW_EH_PE_sdata2          0x0A A 2 bytes signed value.
  83  * DW_EH_PE_sdata4          0x0B A 4 bytes signed value.
  84  * DW_EH_PE_sdata8          0x0C An 8 bytes signed value.
  85  *
  86  * DWARF Exception Header application
  87  *
  88  * Name     Value Meaning
  89  * DW_EH_PE_absptr         0x00 Value is used with no modification.
  90  * DW_EH_PE_pcrel          0x10 Value is reletive to the location of itself
  91  * DW_EH_PE_textrel        0x20
  92  * DW_EH_PE_datarel        0x30 Value is reletive to the beginning of the
  93  *                              eh_frame_hdr segment ( segment type
  94  *                              PT_GNU_EH_FRAME )
  95  * DW_EH_PE_funcrel        0x40
  96  * DW_EH_PE_aligned        0x50 value is an aligned void*
  97  * DW_EH_PE_indirect       0x80 bit to signal indirection after relocation
  98  * DW_EH_PE_omit           0xff No value is present.
  99  *
 100  */
 101 
 102 uint64_t
 103 uleb_extract(unsigned char *data, uint64_t *dotp)
 104 {
 105         uint64_t        dot = *dotp;
 106         uint64_t        res = 0;
 107         int             more = 1;
 108         int             shift = 0;
 109         int             val;
 110 
 111         data += dot;
 112 
 113         while (more) {



 114                 /*
 115                  * Pull off lower 7 bits
 116                  */
 117                 val = (*data) & 0x7f;
 118 
 119                 /*
 120                  * Add prepend value to head of number.
 121                  */
 122                 res = res | (val << shift);
 123 
 124                 /*
 125                  * Increment shift & dot pointer
 126                  */
 127                 shift += 7;
 128                 dot++;
 129 
 130                 /*
 131                  * Check to see if hi bit is set - if not, this
 132                  * is the last byte.
 133                  */
 134                 more = ((*data++) & 0x80) >> 7;
 135         }
 136         *dotp = dot;
 137         return (res);

 138 }
 139 
 140 int64_t
 141 sleb_extract(unsigned char *data, uint64_t *dotp)
 142 {
 143         uint64_t        dot = *dotp;
 144         int64_t         res = 0;
 145         int             more = 1;
 146         int             shift = 0;
 147         int             val;
 148 
 149         data += dot;
 150 
 151         while (more) {



 152                 /*
 153                  * Pull off lower 7 bits
 154                  */
 155                 val = (*data) & 0x7f;
 156 
 157                 /*
 158                  * Add prepend value to head of number.
 159                  */
 160                 res = res | (val << shift);
 161 
 162                 /*
 163                  * Increment shift & dot pointer
 164                  */
 165                 shift += 7;
 166                 dot++;
 167 
 168                 /*
 169                  * Check to see if hi bit is set - if not, this
 170                  * is the last byte.
 171                  */
 172                 more = ((*data++) & 0x80) >> 7;
 173         }
 174         *dotp = dot;
 175 
 176         /*
 177          * Make sure value is properly sign extended.
 178          */
 179         res = (res << (64 - shift)) >> (64 - shift);
 180 
 181         return (res);
 182 }
 183 
 184 /*
 185  * Extract a DWARF encoded datum
 186  *
 187  * entry:
 188  *      data - Base of data buffer containing encoded bytes
 189  *      dotp - Address of variable containing index within data
 190  *              at which the desired datum starts.
 191  *      ehe_flags - DWARF encoding
 192  *      eident - ELF header e_ident[] array for object being processed
 193  *      frame_hdr - Boolean, true if we're extracting from .eh_frame_hdr
 194  *      sh_base - Base address of ELF section containing desired datum
 195  *      sh_offset - Offset relative to sh_base of desired datum.
 196  *      dbase - The base address to which DW_EH_PE_datarel is relative
 197  *              (if frame_hdr is false)
 198  */
 199 uint64_t
 200 dwarf_ehe_extract(unsigned char *data, uint64_t *dotp, uint_t ehe_flags,
 201     unsigned char *eident, boolean_t frame_hdr, uint64_t sh_base,
 202     uint64_t sh_offset, uint64_t dbase)

 203 {
 204         uint64_t    dot = *dotp;
 205         uint_t      lsb;
 206         uint_t      wordsize;
 207         uint_t      fsize;
 208         uint64_t    result;
 209 
 210         if (eident[EI_DATA] == ELFDATA2LSB)
 211                 lsb = 1;
 212         else
 213                 lsb = 0;
 214 
 215         if (eident[EI_CLASS] == ELFCLASS64)
 216                 wordsize = 8;
 217         else
 218                 wordsize = 4;
 219 
 220         switch (ehe_flags & 0x0f) {
 221         case DW_EH_PE_omit:
 222                 return (0);

 223         case DW_EH_PE_absptr:
 224                 fsize = wordsize;
 225                 break;
 226         case DW_EH_PE_udata8:
 227         case DW_EH_PE_sdata8:
 228                 fsize = 8;
 229                 break;
 230         case DW_EH_PE_udata4:
 231         case DW_EH_PE_sdata4:
 232                 fsize = 4;
 233                 break;
 234         case DW_EH_PE_udata2:
 235         case DW_EH_PE_sdata2:
 236                 fsize = 2;
 237                 break;
 238         case DW_EH_PE_uleb128:
 239                 return (uleb_extract(data, dotp));
 240         case DW_EH_PE_sleb128:
 241                 return ((uint64_t)sleb_extract(data, dotp));
 242         default:
 243                 return (0);

 244         }
 245 
 246         if (lsb) {
 247                 /*
 248                  * Extract unaligned LSB formated data
 249                  */
 250                 uint_t  cnt;
 251 
 252                 result = 0;
 253                 for (cnt = 0; cnt < fsize;
 254                     cnt++, dot++) {
 255                         uint64_t val;



 256                         val = data[dot];
 257                         result |= val << (cnt * 8);
 258                 }
 259         } else {
 260                 /*
 261                  * Extract unaligned MSB formated data
 262                  */
 263                 uint_t  cnt;
 264                 result = 0;
 265                 for (cnt = 0; cnt < fsize;
 266                     cnt++, dot++) {
 267                         uint64_t        val;



 268                         val = data[dot];
 269                         result |= val << ((fsize - cnt - 1) * 8);
 270                 }
 271         }
 272         /*
 273          * perform sign extension
 274          */
 275         if ((ehe_flags & DW_EH_PE_signed) &&
 276             (fsize < sizeof (uint64_t))) {
 277                 int64_t sresult;
 278                 uint_t  bitshift;
 279                 sresult = result;
 280                 bitshift = (sizeof (uint64_t) - fsize) * 8;
 281                 sresult = (sresult << bitshift) >> bitshift;
 282                 result = sresult;
 283         }
 284 
 285         /*
 286          * If value is relative to a base address, adjust it
 287          */


 290                 result += sh_base + sh_offset;
 291                 break;
 292 
 293         /*
 294          * datarel is relative to .eh_frame_hdr if within .eh_frame,
 295          * but GOT if not.
 296          */
 297         case DW_EH_PE_datarel:
 298                 if (frame_hdr)
 299                         result += sh_base;
 300                 else
 301                         result += dbase;
 302                 break;
 303         }
 304 
 305         /* Truncate the result to its specified size */
 306         result = (result << ((sizeof (uint64_t) - fsize) * 8)) >>
 307             ((sizeof (uint64_t) - fsize) * 8);
 308 
 309         *dotp = dot;
 310         return (result);

 311 }


  82  * DW_EH_PE_sdata2          0x0A A 2 bytes signed value.
  83  * DW_EH_PE_sdata4          0x0B A 4 bytes signed value.
  84  * DW_EH_PE_sdata8          0x0C An 8 bytes signed value.
  85  *
  86  * DWARF Exception Header application
  87  *
  88  * Name     Value Meaning
  89  * DW_EH_PE_absptr         0x00 Value is used with no modification.
  90  * DW_EH_PE_pcrel          0x10 Value is reletive to the location of itself
  91  * DW_EH_PE_textrel        0x20
  92  * DW_EH_PE_datarel        0x30 Value is reletive to the beginning of the
  93  *                              eh_frame_hdr segment ( segment type
  94  *                              PT_GNU_EH_FRAME )
  95  * DW_EH_PE_funcrel        0x40
  96  * DW_EH_PE_aligned        0x50 value is an aligned void*
  97  * DW_EH_PE_indirect       0x80 bit to signal indirection after relocation
  98  * DW_EH_PE_omit           0xff No value is present.
  99  *
 100  */
 101 
 102 dwarf_error_t
 103 uleb_extract(unsigned char *data, uint64_t *dotp, size_t len, uint64_t *ret)
 104 {
 105         uint64_t        dot = *dotp;
 106         uint64_t        res = 0;
 107         int             more = 1;
 108         int             shift = 0;
 109         int             val;
 110 
 111         data += dot;
 112 
 113         while (more) {
 114                 if (dot > len)
 115                         return (DW_OVERFLOW);
 116 
 117                 /*
 118                  * Pull off lower 7 bits
 119                  */
 120                 val = (*data) & 0x7f;
 121 
 122                 /*
 123                  * Add prepend value to head of number.
 124                  */
 125                 res = res | (val << shift);
 126 
 127                 /*
 128                  * Increment shift & dot pointer
 129                  */
 130                 shift += 7;
 131                 dot++;
 132 
 133                 /*
 134                  * Check to see if hi bit is set - if not, this
 135                  * is the last byte.
 136                  */
 137                 more = ((*data++) & 0x80) >> 7;
 138         }
 139         *dotp = dot;
 140         *ret = res;
 141         return (DW_SUCCESS);
 142 }
 143 
 144 dwarf_error_t
 145 sleb_extract(unsigned char *data, uint64_t *dotp, size_t len, int64_t *ret)
 146 {
 147         uint64_t        dot = *dotp;
 148         int64_t         res = 0;
 149         int             more = 1;
 150         int             shift = 0;
 151         int             val;
 152 
 153         data += dot;
 154 
 155         while (more) {
 156                 if (dot > len)
 157                         return (DW_OVERFLOW);
 158 
 159                 /*
 160                  * Pull off lower 7 bits
 161                  */
 162                 val = (*data) & 0x7f;
 163 
 164                 /*
 165                  * Add prepend value to head of number.
 166                  */
 167                 res = res | (val << shift);
 168 
 169                 /*
 170                  * Increment shift & dot pointer
 171                  */
 172                 shift += 7;
 173                 dot++;
 174 
 175                 /*
 176                  * Check to see if hi bit is set - if not, this
 177                  * is the last byte.
 178                  */
 179                 more = ((*data++) & 0x80) >> 7;
 180         }
 181         *dotp = dot;
 182 
 183         /*
 184          * Make sure value is properly sign extended.
 185          */
 186         res = (res << (64 - shift)) >> (64 - shift);
 187         *ret = res;
 188         return (DW_SUCCESS);
 189 }
 190 
 191 /*
 192  * Extract a DWARF encoded datum
 193  *
 194  * entry:
 195  *      data - Base of data buffer containing encoded bytes
 196  *      dotp - Address of variable containing index within data
 197  *              at which the desired datum starts.
 198  *      ehe_flags - DWARF encoding
 199  *      eident - ELF header e_ident[] array for object being processed
 200  *      frame_hdr - Boolean, true if we're extracting from .eh_frame_hdr
 201  *      sh_base - Base address of ELF section containing desired datum
 202  *      sh_offset - Offset relative to sh_base of desired datum.
 203  *      dbase - The base address to which DW_EH_PE_datarel is relative
 204  *              (if frame_hdr is false)
 205  */
 206 dwarf_error_t
 207 dwarf_ehe_extract(unsigned char *data, size_t len, uint64_t *dotp,
 208     uint64_t *ret, uint_t ehe_flags, unsigned char *eident,
 209     boolean_t frame_hdr, uint64_t sh_base, uint64_t sh_offset,
 210     uint64_t dbase)
 211 {
 212         uint64_t    dot = *dotp;
 213         uint_t      lsb;
 214         uint_t      wordsize;
 215         uint_t      fsize;
 216         uint64_t    result;
 217 
 218         if (eident[EI_DATA] == ELFDATA2LSB)
 219                 lsb = 1;
 220         else
 221                 lsb = 0;
 222 
 223         if (eident[EI_CLASS] == ELFCLASS64)
 224                 wordsize = 8;
 225         else
 226                 wordsize = 4;
 227 
 228         switch (ehe_flags & 0x0f) {
 229         case DW_EH_PE_omit:
 230                 *ret = 0;
 231                 return (DW_SUCCESS);
 232         case DW_EH_PE_absptr:
 233                 fsize = wordsize;
 234                 break;
 235         case DW_EH_PE_udata8:
 236         case DW_EH_PE_sdata8:
 237                 fsize = 8;
 238                 break;
 239         case DW_EH_PE_udata4:
 240         case DW_EH_PE_sdata4:
 241                 fsize = 4;
 242                 break;
 243         case DW_EH_PE_udata2:
 244         case DW_EH_PE_sdata2:
 245                 fsize = 2;
 246                 break;
 247         case DW_EH_PE_uleb128:
 248                 return (uleb_extract(data, dotp, len, ret));
 249         case DW_EH_PE_sleb128:
 250                 return (sleb_extract(data, dotp, len, (int64_t *)ret));
 251         default:
 252                 *ret = 0;
 253                 return (DW_BAD_ENCODING);
 254         }
 255 
 256         if (lsb) {
 257                 /*
 258                  * Extract unaligned LSB formated data
 259                  */
 260                 uint_t  cnt;
 261 
 262                 result = 0;
 263                 for (cnt = 0; cnt < fsize;
 264                     cnt++, dot++) {
 265                         uint64_t val;
 266 
 267                         if (dot > len)
 268                                 return (DW_OVERFLOW);
 269                         val = data[dot];
 270                         result |= val << (cnt * 8);
 271                 }
 272         } else {
 273                 /*
 274                  * Extract unaligned MSB formated data
 275                  */
 276                 uint_t  cnt;
 277                 result = 0;
 278                 for (cnt = 0; cnt < fsize;
 279                     cnt++, dot++) {
 280                         uint64_t val;
 281 
 282                         if (dot > len)
 283                                 return (DW_OVERFLOW);
 284                         val = data[dot];
 285                         result |= val << ((fsize - cnt - 1) * 8);
 286                 }
 287         }
 288         /*
 289          * perform sign extension
 290          */
 291         if ((ehe_flags & DW_EH_PE_signed) &&
 292             (fsize < sizeof (uint64_t))) {
 293                 int64_t sresult;
 294                 uint_t  bitshift;
 295                 sresult = result;
 296                 bitshift = (sizeof (uint64_t) - fsize) * 8;
 297                 sresult = (sresult << bitshift) >> bitshift;
 298                 result = sresult;
 299         }
 300 
 301         /*
 302          * If value is relative to a base address, adjust it
 303          */


 306                 result += sh_base + sh_offset;
 307                 break;
 308 
 309         /*
 310          * datarel is relative to .eh_frame_hdr if within .eh_frame,
 311          * but GOT if not.
 312          */
 313         case DW_EH_PE_datarel:
 314                 if (frame_hdr)
 315                         result += sh_base;
 316                 else
 317                         result += dbase;
 318                 break;
 319         }
 320 
 321         /* Truncate the result to its specified size */
 322         result = (result << ((sizeof (uint64_t) - fsize) * 8)) >>
 323             ((sizeof (uint64_t) - fsize) * 8);
 324 
 325         *dotp = dot;
 326         *ret = result;
 327         return (DW_SUCCESS);
 328 }