Page 1 of 1

What to do with ELF '.group' sections

Posted: Sat Feb 19, 2011 12:25 pm
by blobmiester
I'm working on my ELF linker to load relocatable ELF files for my modular kernel. I've implemented relocation and got it working. Then I added some code to work with a c++ class in my module and the linker blew up. I tried running readelf and there is about 150 '.group' sections now. Along with several sections that have a sh_addr of 0 (I link with a base address of 0) and a sh_size of not zero.

First, I just sequentially added 0-offset sections to the allocated virtual address and then relocate them (because the next section is a .rel section).

But that still doesn't work. I feel like I'm missing something with this whole ELF loader. Like I've done it wrong from the beginning. The '.group' sections have a type of 11 or DYNSYM. However there is no other DYNx sections in the file.

Anyway, any help on the right track would be much appreciated.

First, here is the readelf output of the module I'm trying to load:

Code: Select all

There are 393 section headers, starting at offset 0x1d10:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .group            GROUP           00000000 000034 000008 04     391 438  4
  [ 2] .group            GROUP           00000000 00003c 000008 04     391 393  4
  [ 3] .group            GROUP           00000000 000044 000008 04     391 548  4
  [ 4] .group            GROUP           00000000 00004c 00000c 04     391 326  4
  [ 5] .group            GROUP           00000000 000058 00000c 04     391 327  4
  [ 6] .group            GROUP           00000000 000064 00000c 04     391 328  4
  [ 7] .group            GROUP           00000000 000070 00000c 04     391 329  4
  [ 8] .group            GROUP           00000000 00007c 000008 04     391 397  4
  [ 9] .group            GROUP           00000000 000084 000008 04     391 366  4
  [10] .group            GROUP           00000000 00008c 000008 04     391 500  4
  [11] .group            GROUP           00000000 000094 000008 04     391 418  4
  [12] .group            GROUP           00000000 00009c 000008 04     391 543  4
  [13] .group            GROUP           00000000 0000a4 000008 04     391 549  4
  [14] .group            GROUP           00000000 0000ac 000008 04     391 557  4
  [15] .group            GROUP           00000000 0000b4 000008 04     391 411  4
  [16] .group            GROUP           00000000 0000bc 000008 04     391 402  4
  [17] .group            GROUP           00000000 0000c4 000008 04     391 508  4
  [18] .group            GROUP           00000000 0000cc 000008 04     391 477  4
  [19] .group            GROUP           00000000 0000d4 000008 04     391 503  4
  [20] .group            GROUP           00000000 0000dc 000008 04     391 494  4
  [21] .group            GROUP           00000000 0000e4 000008 04     391 547  4
  [22] .group            GROUP           00000000 0000ec 000008 04     391 485  4
  [23] .group            GROUP           00000000 0000f4 000008 04     391 528  4
  [24] .group            GROUP           00000000 0000fc 000008 04     391 420  4
  [25] .group            GROUP           00000000 000104 000008 04     391 465  4
  [26] .group            GROUP           00000000 00010c 000008 04     391 419  4
  [27] .group            GROUP           00000000 000114 000008 04     391 409  4
  [28] .group            GROUP           00000000 00011c 000008 04     391 441  4
  [29] .group            GROUP           00000000 000124 000008 04     391 480  4
  [30] .group            GROUP           00000000 00012c 000008 04     391 353  4
  [31] .group            GROUP           00000000 000134 000008 04     391 417  4
  [32] .group            GROUP           00000000 00013c 000008 04     391 463  4
  [33] .group            GROUP           00000000 000144 000008 04     391 545  4
  [34] .group            GROUP           00000000 00014c 000008 04     391 429  4
  [35] .group            GROUP           00000000 000154 000008 04     391 491  4
  [36] .group            GROUP           00000000 00015c 000008 04     391 454  4
  [37] .group            GROUP           00000000 000164 000008 04     391 512  4
  [38] .group            GROUP           00000000 00016c 000008 04     391 392  4
  [39] .group            GROUP           00000000 000174 000008 04     391 385  4
  [40] .group            GROUP           00000000 00017c 000008 04     391 540  4
  [41] .group            GROUP           00000000 000184 000008 04     391 468  4
  [42] .group            GROUP           00000000 00018c 000008 04     391 518  4
  [43] .group            GROUP           00000000 000194 000008 04     391 464  4
  [44] .group            GROUP           00000000 00019c 000008 04     391 355  4
  [45] .group            GROUP           00000000 0001a4 000008 04     391 435  4
  [46] .group            GROUP           00000000 0001ac 000008 04     391 479  4
  [47] .group            GROUP           00000000 0001b4 000008 04     391 376  4
  [48] .group            GROUP           00000000 0001bc 000008 04     391 527  4
  [49] .group            GROUP           00000000 0001c4 000008 04     391 386  4
  [50] .group            GROUP           00000000 0001cc 000008 04     391 498  4
  [51] .group            GROUP           00000000 0001d4 000008 04     391 444  4
  [52] .group            GROUP           00000000 0001dc 000008 04     391 551  4
  [53] .group            GROUP           00000000 0001e4 000008 04     391 529  4
  [54] .group            GROUP           00000000 0001ec 000008 04     391 460  4
  [55] .group            GROUP           00000000 0001f4 000008 04     391 462  4
  [56] .group            GROUP           00000000 0001fc 000008 04     391 516  4
  [57] .group            GROUP           00000000 000204 000008 04     391 552  4
  [58] .group            GROUP           00000000 00020c 000008 04     391 352  4
  [59] .group            GROUP           00000000 000214 000008 04     391 489  4
  [60] .group            GROUP           00000000 00021c 000008 04     391 499  4
  [61] .group            GROUP           00000000 000224 000008 04     391 351  4
  [62] .group            GROUP           00000000 00022c 000008 04     391 492  4
  [63] .group            GROUP           00000000 000234 000008 04     391 408  4
  [64] .group            GROUP           00000000 00023c 000008 04     391 360  4
  [65] .group            GROUP           00000000 000244 000008 04     391 348  4
  [66] .group            GROUP           00000000 00024c 000008 04     391 554  4
  [67] .group            GROUP           00000000 000254 000008 04     391 459  4
  [68] .group            GROUP           00000000 00025c 000008 04     391 426  4
  [69] .group            GROUP           00000000 000264 000008 04     391 461  4
  [70] .group            GROUP           00000000 00026c 000008 04     391 350  4
  [71] .group            GROUP           00000000 000274 000008 04     391 381  4
  [72] .group            GROUP           00000000 00027c 000008 04     391 415  4
  [73] .group            GROUP           00000000 000284 000008 04     391 389  4
  [74] .group            GROUP           00000000 00028c 000008 04     391 522  4
  [75] .group            GROUP           00000000 000294 000008 04     391 388  4
  [76] .group            GROUP           00000000 00029c 000008 04     391 534  4
  [77] .group            GROUP           00000000 0002a4 000008 04     391 513  4
  [78] .group            GROUP           00000000 0002ac 000008 04     391 405  4
  [79] .group            GROUP           00000000 0002b4 000008 04     391 410  4
  [80] .group            GROUP           00000000 0002bc 000008 04     391 423  4
  [81] .group            GROUP           00000000 0002c4 000008 04     391 470  4
  [82] .group            GROUP           00000000 0002cc 000008 04     391 422  4
  [83] .group            GROUP           00000000 0002d4 000008 04     391 467  4
  [84] .group            GROUP           00000000 0002dc 000008 04     391 510  4
  [85] .group            GROUP           00000000 0002e4 000008 04     391 519  4
  [86] .group            GROUP           00000000 0002ec 000008 04     391 514  4
  [87] .group            GROUP           00000000 0002f4 000008 04     391 361  4
  [88] .group            GROUP           00000000 0002fc 000008 04     391 541  4
  [89] .group            GROUP           00000000 000304 000008 04     391 431  4
  [90] .group            GROUP           00000000 00030c 000008 04     391 432  4
  [91] .group            GROUP           00000000 000314 000008 04     391 428  4
  [92] .group            GROUP           00000000 00031c 000008 04     391 450  4
  [93] .group            GROUP           00000000 000324 000008 04     391 523  4
  [94] .group            GROUP           00000000 00032c 000008 04     391 505  4
  [95] .group            GROUP           00000000 000334 000008 04     391 483  4
  [96] .group            GROUP           00000000 00033c 000008 04     391 403  4
  [97] .group            GROUP           00000000 000344 000008 04     391 427  4
  [98] .group            GROUP           00000000 00034c 000008 04     391 539  4
  [99] .group            GROUP           00000000 000354 000008 04     391 471  4
  [100] .group            GROUP           00000000 00035c 000008 04     391 425  4
  [101] .group            GROUP           00000000 000364 000008 04     391 488  4
  [102] .group            GROUP           00000000 00036c 000008 04     391 375  4
  [103] .group            GROUP           00000000 000374 000008 04     391 413  4
  [104] .group            GROUP           00000000 00037c 000008 04     391 532  4
  [105] .group            GROUP           00000000 000384 000008 04     391 448  4
  [106] .group            GROUP           00000000 00038c 000008 04     391 359  4
  [107] .group            GROUP           00000000 000394 000008 04     391 550  4
  [108] .group            GROUP           00000000 00039c 000008 04     391 407  4
  [109] .group            GROUP           00000000 0003a4 000008 04     391 362  4
  [110] .group            GROUP           00000000 0003ac 000008 04     391 434  4
  [111] .group            GROUP           00000000 0003b4 000008 04     391 383  4
  [112] .group            GROUP           00000000 0003bc 000008 04     391 442  4
  [113] .group            GROUP           00000000 0003c4 000008 04     391 533  4
  [114] .group            GROUP           00000000 0003cc 000008 04     391 452  4
  [115] .group            GROUP           00000000 0003d4 000008 04     391 357  4
  [116] .group            GROUP           00000000 0003dc 000008 04     391 367  4
  [117] .group            GROUP           00000000 0003e4 000008 04     391 414  4
  [118] .group            GROUP           00000000 0003ec 000008 04     391 424  4
  [119] .group            GROUP           00000000 0003f4 000008 04     391 531  4
  [120] .group            GROUP           00000000 0003fc 000008 04     391 563  4
  [121] .group            GROUP           00000000 000404 000008 04     391 398  4
  [122] .group            GROUP           00000000 00040c 000008 04     391 526  4
  [123] .group            GROUP           00000000 000414 000008 04     391 562  4
  [124] .group            GROUP           00000000 00041c 000008 04     391 535  4
  [125] .group            GROUP           00000000 000424 000008 04     391 421  4
  [126] .group            GROUP           00000000 00042c 000008 04     391 521  4
  [127] .group            GROUP           00000000 000434 000008 04     391 561  4
  [128] .group            GROUP           00000000 00043c 000008 04     391 455  4
  [129] .group            GROUP           00000000 000444 000008 04     391 473  4
  [130] .group            GROUP           00000000 00044c 000008 04     391 377  4
  [131] .group            GROUP           00000000 000454 000008 04     391 354  4
  [132] .group            GROUP           00000000 00045c 000008 04     391 373  4
  [133] .group            GROUP           00000000 000464 000008 04     391 506  4
  [134] .group            GROUP           00000000 00046c 000008 04     391 451  4
  [135] .group            GROUP           00000000 000474 000008 04     391 390  4
  [136] .group            GROUP           00000000 00047c 000008 04     391 371  4
  [137] .group            GROUP           00000000 000484 000008 04     391 497  4
  [138] .group            GROUP           00000000 00048c 000008 04     391 443  4
  [139] .group            GROUP           00000000 000494 000008 04     391 437  4
  [140] .group            GROUP           00000000 00049c 000008 04     391 447  4
  [141] .group            GROUP           00000000 0004a4 000008 04     391 553  4
  [142] .group            GROUP           00000000 0004ac 000008 04     391 430  4
  [143] .group            GROUP           00000000 0004b4 000008 04     391 504  4
  [144] .group            GROUP           00000000 0004bc 000008 04     391 374  4
  [145] .group            GROUP           00000000 0004c4 000008 04     391 458  4
  [146] .group            GROUP           00000000 0004cc 000008 04     391 372  4
  [147] .group            GROUP           00000000 0004d4 000008 04     391 380  4
  [148] .group            GROUP           00000000 0004dc 000008 04     391 387  4
  [149] .group            GROUP           00000000 0004e4 000008 04     391 524  4
  [150] .text             PROGBITS        00000000 0004f0 000627 00  AX  0   0 16
  [151] .rel.text         REL             00000000 005a78 000150 08     391 150  4
  [152] .text._ZNSt9type_ PROGBITS        00000000 000b18 00000e 00 AXG  0   0  2
  [153] .rel.text._ZNSt9t REL             00000000 005bc8 000008 08     391 152  4
  [154] .text._ZN10__cxxa PROGBITS        00000000 000b26 00000e 00 AXG  0   0  2
  [155] .rel.text._ZN10__ REL             00000000 005bd0 000008 08     391 154  4
  [156] .text._ZN10__cxxa PROGBITS        00000000 000b34 00000e 00 AXG  0   0  2
  [157] .rel.text._ZN10__ REL             00000000 005bd8 000008 08     391 156  4
  [158] .text._ZN10__cxxa PROGBITS        00000000 000b42 00000e 00 AXG  0   0  2
  [159] .rel.text._ZN10__ REL             00000000 005be0 000008 08     391 158  4
  [160] .text._ZN10__cxxa PROGBITS        00000000 000b50 000015 00 AXG  0   0  2
  [161] .rel.text._ZN10__ REL             00000000 005be8 000010 08     391 160  4
  [162] .text._ZN10__cxxa PROGBITS        00000000 000b66 000015 00 AXG  0   0  2
  [163] .rel.text._ZN10__ REL             00000000 005bf8 000010 08     391 162  4
  [164] .text._ZN10__cxxa PROGBITS        00000000 000b7c 000015 00 AXG  0   0  2
  [165] .rel.text._ZN10__ REL             00000000 005c08 000010 08     391 164  4
  [166] .text._ZNSt9type_ PROGBITS        00000000 000b92 000015 00 AXG  0   0  2
  [167] .rel.text._ZNSt9t REL             00000000 005c18 000010 08     391 166  4
  [168] .rodata           PROGBITS        00001000 000ba7 000000 00  WA  0   0  1
  [169] .rodata._ZTVN6sys PROGBITS        00000000 000ba8 000018 00  AG  0   0  8
  [170] .rel.rodata._ZTVN REL             00000000 005c28 000020 08     391 169  4
  [171] .rodata._ZTIPKDn  PROGBITS        00000000 000bc0 000010 00  AG  0   0  4
  [172] .rel.rodata._ZTIP REL             00000000 005c48 000018 08     391 171  4
  [173] .rodata._ZTIPDn   PROGBITS        00000000 000bd0 000010 00  AG  0   0  4
  [174] .rel.rodata._ZTIP REL             00000000 005c60 000018 08     391 173  4
  [175] .rodata._ZTIDn    PROGBITS        00000000 000be0 000008 00  AG  0   0  4
  [176] .rel.rodata._ZTID REL             00000000 005c78 000010 08     391 175  4
  [177] .rodata._ZTIPKDe  PROGBITS        00000000 000be8 000010 00  AG  0   0  4
  [178] .rel.rodata._ZTIP REL             00000000 005c88 000018 08     391 177  4
  [179] .rodata._ZTIPDe   PROGBITS        00000000 000bf8 000010 00  AG  0   0  4
  [180] .rel.rodata._ZTIP REL             00000000 005ca0 000018 08     391 179  4
  [181] .rodata._ZTIDe    PROGBITS        00000000 000c08 000008 00  AG  0   0  4
  [182] .rel.rodata._ZTID REL             00000000 005cb8 000010 08     391 181  4
  [183] .rodata._ZTIPKDd  PROGBITS        00000000 000c10 000010 00  AG  0   0  4
  [184] .rel.rodata._ZTIP REL             00000000 005cc8 000018 08     391 183  4
  [185] .rodata._ZTIPDd   PROGBITS        00000000 000c20 000010 00  AG  0   0  4
  [186] .rel.rodata._ZTIP REL             00000000 005ce0 000018 08     391 185  4
  [187] .rodata._ZTIDd    PROGBITS        00000000 000c30 000008 00  AG  0   0  4
  [188] .rel.rodata._ZTID REL             00000000 005cf8 000010 08     391 187  4
  [189] .rodata._ZTIPKDf  PROGBITS        00000000 000c38 000010 00  AG  0   0  4
  [190] .rel.rodata._ZTIP REL             00000000 005d08 000018 08     391 189  4
  [191] .rodata._ZTIPDf   PROGBITS        00000000 000c48 000010 00  AG  0   0  4
  [192] .rel.rodata._ZTIP REL             00000000 005d20 000018 08     391 191  4
  [193] .rodata._ZTIDf    PROGBITS        00000000 000c58 000008 00  AG  0   0  4
  [194] .rel.rodata._ZTID REL             00000000 005d38 000010 08     391 193  4
  [195] .rodata._ZTIPKe   PROGBITS        00000000 000c60 000010 00  AG  0   0  4
  [196] .rel.rodata._ZTIP REL             00000000 005d48 000018 08     391 195  4
  [197] .rodata._ZTIPe    PROGBITS        00000000 000c70 000010 00  AG  0   0  4
  [198] .rel.rodata._ZTIP REL             00000000 005d60 000018 08     391 197  4
  [199] .rodata._ZTIe     PROGBITS        00000000 000c80 000008 00  AG  0   0  4
  [200] .rel.rodata._ZTIe REL             00000000 005d78 000010 08     391 199  4
  [201] .rodata._ZTIPKd   PROGBITS        00000000 000c88 000010 00  AG  0   0  4
  [202] .rel.rodata._ZTIP REL             00000000 005d88 000018 08     391 201  4
  [203] .rodata._ZTIPd    PROGBITS        00000000 000c98 000010 00  AG  0   0  4
  [204] .rel.rodata._ZTIP REL             00000000 005da0 000018 08     391 203  4
  [205] .rodata._ZTId     PROGBITS        00000000 000ca8 000008 00  AG  0   0  4
  [206] .rel.rodata._ZTId REL             00000000 005db8 000010 08     391 205  4
  [207] .rodata._ZTIPKf   PROGBITS        00000000 000cb0 000010 00  AG  0   0  4
  [208] .rel.rodata._ZTIP REL             00000000 005dc8 000018 08     391 207  4
  [209] .rodata._ZTIPf    PROGBITS        00000000 000cc0 000010 00  AG  0   0  4
  [210] .rel.rodata._ZTIP REL             00000000 005de0 000018 08     391 209  4
  [211] .rodata._ZTIf     PROGBITS        00000000 000cd0 000008 00  AG  0   0  4
  [212] .rel.rodata._ZTIf REL             00000000 005df8 000010 08     391 211  4
  [213] .rodata._ZTIPKy   PROGBITS        00000000 000cd8 000010 00  AG  0   0  4
  [214] .rel.rodata._ZTIP REL             00000000 005e08 000018 08     391 213  4
  [215] .rodata._ZTIPy    PROGBITS        00000000 000ce8 000010 00  AG  0   0  4
  [216] .rel.rodata._ZTIP REL             00000000 005e20 000018 08     391 215  4
  [217] .rodata._ZTIy     PROGBITS        00000000 000cf8 000008 00  AG  0   0  4
  [218] .rel.rodata._ZTIy REL             00000000 005e38 000010 08     391 217  4
  [219] .rodata._ZTIPKx   PROGBITS        00000000 000d00 000010 00  AG  0   0  4
  [220] .rel.rodata._ZTIP REL             00000000 005e48 000018 08     391 219  4
  [221] .rodata._ZTIPx    PROGBITS        00000000 000d10 000010 00  AG  0   0  4
  [222] .rel.rodata._ZTIP REL             00000000 005e60 000018 08     391 221  4
  [223] .rodata._ZTIx     PROGBITS        00000000 000d20 000008 00  AG  0   0  4
  [224] .rel.rodata._ZTIx REL             00000000 005e78 000010 08     391 223  4
  [225] .rodata._ZTIPKm   PROGBITS        00000000 000d28 000010 00  AG  0   0  4
  [226] .rel.rodata._ZTIP REL             00000000 005e88 000018 08     391 225  4
  [227] .rodata._ZTIPm    PROGBITS        00000000 000d38 000010 00  AG  0   0  4
  [228] .rel.rodata._ZTIP REL             00000000 005ea0 000018 08     391 227  4
  [229] .rodata._ZTIm     PROGBITS        00000000 000d48 000008 00  AG  0   0  4
  [230] .rel.rodata._ZTIm REL             00000000 005eb8 000010 08     391 229  4
  [231] .rodata._ZTIPKl   PROGBITS        00000000 000d50 000010 00  AG  0   0  4
  [232] .rel.rodata._ZTIP REL             00000000 005ec8 000018 08     391 231  4
  [233] .rodata._ZTIPl    PROGBITS        00000000 000d60 000010 00  AG  0   0  4
  [234] .rel.rodata._ZTIP REL             00000000 005ee0 000018 08     391 233  4
  [235] .rodata._ZTIl     PROGBITS        00000000 000d70 000008 00  AG  0   0  4
  [236] .rel.rodata._ZTIl REL             00000000 005ef8 000010 08     391 235  4
  [237] .rodata._ZTIPKj   PROGBITS        00000000 000d78 000010 00  AG  0   0  4
  [238] .rel.rodata._ZTIP REL             00000000 005f08 000018 08     391 237  4
  [239] .rodata._ZTIPj    PROGBITS        00000000 000d88 000010 00  AG  0   0  4
  [240] .rel.rodata._ZTIP REL             00000000 005f20 000018 08     391 239  4
  [241] .rodata._ZTIj     PROGBITS        00000000 000d98 000008 00  AG  0   0  4
  [242] .rel.rodata._ZTIj REL             00000000 005f38 000010 08     391 241  4
  [243] .rodata._ZTIPKi   PROGBITS        00000000 000da0 000010 00  AG  0   0  4
  [244] .rel.rodata._ZTIP REL             00000000 005f48 000018 08     391 243  4
  [245] .rodata._ZTIPi    PROGBITS        00000000 000db0 000010 00  AG  0   0  4
  [246] .rel.rodata._ZTIP REL             00000000 005f60 000018 08     391 245  4
  [247] .rodata._ZTIi     PROGBITS        00000000 000dc0 000008 00  AG  0   0  4
  [248] .rel.rodata._ZTIi REL             00000000 005f78 000010 08     391 247  4
  [249] .rodata._ZTIPKt   PROGBITS        00000000 000dc8 000010 00  AG  0   0  4
  [250] .rel.rodata._ZTIP REL             00000000 005f88 000018 08     391 249  4
  [251] .rodata._ZTIPt    PROGBITS        00000000 000dd8 000010 00  AG  0   0  4
  [252] .rel.rodata._ZTIP REL             00000000 005fa0 000018 08     391 251  4
  [253] .rodata._ZTIt     PROGBITS        00000000 000de8 000008 00  AG  0   0  4
  [254] .rel.rodata._ZTIt REL             00000000 005fb8 000010 08     391 253  4
  [255] .rodata._ZTIPKs   PROGBITS        00000000 000df0 000010 00  AG  0   0  4
  [256] .rel.rodata._ZTIP REL             00000000 005fc8 000018 08     391 255  4
  [257] .rodata._ZTIPs    PROGBITS        00000000 000e00 000010 00  AG  0   0  4
  [258] .rel.rodata._ZTIP REL             00000000 005fe0 000018 08     391 257  4
  [259] .rodata._ZTIs     PROGBITS        00000000 000e10 000008 00  AG  0   0  4
  [260] .rel.rodata._ZTIs REL             00000000 005ff8 000010 08     391 259  4
  [261] .rodata._ZTIPKh   PROGBITS        00000000 000e18 000010 00  AG  0   0  4
  [262] .rel.rodata._ZTIP REL             00000000 006008 000018 08     391 261  4
  [263] .rodata._ZTIPh    PROGBITS        00000000 000e28 000010 00  AG  0   0  4
  [264] .rel.rodata._ZTIP REL             00000000 006020 000018 08     391 263  4
  [265] .rodata._ZTIh     PROGBITS        00000000 000e38 000008 00  AG  0   0  4
  [266] .rel.rodata._ZTIh REL             00000000 006038 000010 08     391 265  4
  [267] .rodata._ZTIPKa   PROGBITS        00000000 000e40 000010 00  AG  0   0  4
  [268] .rel.rodata._ZTIP REL             00000000 006048 000018 08     391 267  4
  [269] .rodata._ZTIPa    PROGBITS        00000000 000e50 000010 00  AG  0   0  4
  [270] .rel.rodata._ZTIP REL             00000000 006060 000018 08     391 269  4
  [271] .rodata._ZTIa     PROGBITS        00000000 000e60 000008 00  AG  0   0  4
  [272] .rel.rodata._ZTIa REL             00000000 006078 000010 08     391 271  4
  [273] .rodata._ZTIPKc   PROGBITS        00000000 000e68 000010 00  AG  0   0  4
  [274] .rel.rodata._ZTIP REL             00000000 006088 000018 08     391 273  4
  [275] .rodata._ZTIPc    PROGBITS        00000000 000e78 000010 00  AG  0   0  4
  [276] .rel.rodata._ZTIP REL             00000000 0060a0 000018 08     391 275  4
  [277] .rodata._ZTIc     PROGBITS        00000000 000e88 000008 00  AG  0   0  4
  [278] .rel.rodata._ZTIc REL             00000000 0060b8 000010 08     391 277  4
  [279] .rodata._ZTIPKDi  PROGBITS        00000000 000e90 000010 00  AG  0   0  4
  [280] .rel.rodata._ZTIP REL             00000000 0060c8 000018 08     391 279  4
  [281] .rodata._ZTIPDi   PROGBITS        00000000 000ea0 000010 00  AG  0   0  4
  [282] .rel.rodata._ZTIP REL             00000000 0060e0 000018 08     391 281  4
  [283] .rodata._ZTIDi    PROGBITS        00000000 000eb0 000008 00  AG  0   0  4
  [284] .rel.rodata._ZTID REL             00000000 0060f8 000010 08     391 283  4
  [285] .rodata._ZTIPKDs  PROGBITS        00000000 000eb8 000010 00  AG  0   0  4
  [286] .rel.rodata._ZTIP REL             00000000 006108 000018 08     391 285  4
  [287] .rodata._ZTIPDs   PROGBITS        00000000 000ec8 000010 00  AG  0   0  4
  [288] .rel.rodata._ZTIP REL             00000000 006120 000018 08     391 287  4
  [289] .rodata._ZTIDs    PROGBITS        00000000 000ed8 000008 00  AG  0   0  4
  [290] .rel.rodata._ZTID REL             00000000 006138 000010 08     391 289  4
  [291] .rodata._ZTIPKw   PROGBITS        00000000 000ee0 000010 00  AG  0   0  4
  [292] .rel.rodata._ZTIP REL             00000000 006148 000018 08     391 291  4
  [293] .rodata._ZTIPw    PROGBITS        00000000 000ef0 000010 00  AG  0   0  4
  [294] .rel.rodata._ZTIP REL             00000000 006160 000018 08     391 293  4
  [295] .rodata._ZTIw     PROGBITS        00000000 000f00 000008 00  AG  0   0  4
  [296] .rel.rodata._ZTIw REL             00000000 006178 000010 08     391 295  4
  [297] .rodata._ZTIPKb   PROGBITS        00000000 000f08 000010 00  AG  0   0  4
  [298] .rel.rodata._ZTIP REL             00000000 006188 000018 08     391 297  4
  [299] .rodata._ZTIPb    PROGBITS        00000000 000f18 000010 00  AG  0   0  4
  [300] .rel.rodata._ZTIP REL             00000000 0061a0 000018 08     391 299  4
  [301] .rodata._ZTIb     PROGBITS        00000000 000f28 000008 00  AG  0   0  4
  [302] .rel.rodata._ZTIb REL             00000000 0061b8 000010 08     391 301  4
  [303] .rodata._ZTIPKv   PROGBITS        00000000 000f30 000010 00  AG  0   0  4
  [304] .rel.rodata._ZTIP REL             00000000 0061c8 000018 08     391 303  4
  [305] .rodata._ZTIPv    PROGBITS        00000000 000f40 000010 00  AG  0   0  4
  [306] .rel.rodata._ZTIP REL             00000000 0061e0 000018 08     391 305  4
  [307] .rodata._ZTIv     PROGBITS        00000000 000f50 000008 00  AG  0   0  4
  [308] .rel.rodata._ZTIv REL             00000000 0061f8 000010 08     391 307  4
  [309] .rodata._ZTVN10__ PROGBITS        00000000 000f58 000010 00  AG  0   0  8
  [310] .rel.rodata._ZTVN REL             00000000 006208 000010 08     391 309  4
  [311] .rodata._ZTVSt9ty PROGBITS        00000000 000f68 000010 00  AG  0   0  8
  [312] .rel.rodata._ZTVS REL             00000000 006218 000010 08     391 311  4
  [313] .rodata._ZTVN10__ PROGBITS        00000000 000f78 000010 00  AG  0   0  8
  [314] .rel.rodata._ZTVN REL             00000000 006228 000010 08     391 313  4
  [315] .rodata._ZTVN10__ PROGBITS        00000000 000f88 000010 00  AG  0   0  8
  [316] .rel.rodata._ZTVN REL             00000000 006238 000010 08     391 315  4
  [317] .rodata._ZTSPKDn  PROGBITS        00000000 000f98 000005 00  AG  0   0  4
  [318] .rodata._ZTSPDn   PROGBITS        00000000 000fa0 000004 00  AG  0   0  4
  [319] .rodata._ZTSDn    PROGBITS        00000000 000fa4 000003 00  AG  0   0  1
  [320] .rodata._ZTSPKDe  PROGBITS        00000000 000fa8 000005 00  AG  0   0  4
  [321] .rodata._ZTSPDe   PROGBITS        00000000 000fb0 000004 00  AG  0   0  4
  [322] .rodata._ZTSDe    PROGBITS        00000000 000fb4 000003 00  AG  0   0  1
  [323] .rodata._ZTSPKDd  PROGBITS        00000000 000fb8 000005 00  AG  0   0  4
  [324] .rodata._ZTSPDd   PROGBITS        00000000 000fc0 000004 00  AG  0   0  4
  [325] .rodata._ZTSDd    PROGBITS        00000000 000fc4 000003 00  AG  0   0  1
  [326] .rodata._ZTSPKDf  PROGBITS        00000000 000fc8 000005 00  AG  0   0  4
  [327] .rodata._ZTSPDf   PROGBITS        00000000 000fd0 000004 00  AG  0   0  4
  [328] .rodata._ZTSDf    PROGBITS        00000000 000fd4 000003 00  AG  0   0  1
  [329] .rodata._ZTSPKe   PROGBITS        00000000 000fd8 000004 00  AG  0   0  4
  [330] .rodata._ZTSPe    PROGBITS        00000000 000fdc 000003 00  AG  0   0  1
  [331] .rodata._ZTSe     PROGBITS        00000000 000fdf 000002 00  AG  0   0  1
  [332] .rodata._ZTSPKd   PROGBITS        00000000 000fe4 000004 00  AG  0   0  4
  [333] .rodata._ZTSPd    PROGBITS        00000000 000fe8 000003 00  AG  0   0  1
  [334] .rodata._ZTSd     PROGBITS        00000000 000feb 000002 00  AG  0   0  1
  [335] .rodata._ZTSPKf   PROGBITS        00000000 000ff0 000004 00  AG  0   0  4
  [336] .rodata._ZTSPf    PROGBITS        00000000 000ff4 000003 00  AG  0   0  1
  [337] .rodata._ZTSf     PROGBITS        00000000 000ff7 000002 00  AG  0   0  1
  [338] .rodata._ZTSPKy   PROGBITS        00000000 000ffc 000004 00  AG  0   0  4
  [339] .rodata._ZTSPy    PROGBITS        00000000 001000 000003 00  AG  0   0  1
  [340] .rodata._ZTSy     PROGBITS        00000000 001003 000002 00  AG  0   0  1
  [341] .rodata._ZTSPKx   PROGBITS        00000000 001008 000004 00  AG  0   0  4
  [342] .rodata._ZTSPx    PROGBITS        00000000 00100c 000003 00  AG  0   0  1
  [343] .rodata._ZTSx     PROGBITS        00000000 00100f 000002 00  AG  0   0  1
  [344] .rodata._ZTSPKm   PROGBITS        00000000 001014 000004 00  AG  0   0  4
  [345] .rodata._ZTSPm    PROGBITS        00000000 001018 000003 00  AG  0   0  1
  [346] .rodata._ZTSm     PROGBITS        00000000 00101b 000002 00  AG  0   0  1
  [347] .rodata._ZTSPKl   PROGBITS        00000000 001020 000004 00  AG  0   0  4
  [348] .rodata._ZTSPl    PROGBITS        00000000 001024 000003 00  AG  0   0  1
  [349] .rodata._ZTSl     PROGBITS        00000000 001027 000002 00  AG  0   0  1
  [350] .rodata._ZTSPKj   PROGBITS        00000000 00102c 000004 00  AG  0   0  4
  [351] .rodata._ZTSPj    PROGBITS        00000000 001030 000003 00  AG  0   0  1
  [352] .rodata._ZTSj     PROGBITS        00000000 001033 000002 00  AG  0   0  1
  [353] .rodata._ZTSPKi   PROGBITS        00000000 001038 000004 00  AG  0   0  4
  [354] .rodata._ZTSPi    PROGBITS        00000000 00103c 000003 00  AG  0   0  1
  [355] .rodata._ZTSi     PROGBITS        00000000 00103f 000002 00  AG  0   0  1
  [356] .rodata._ZTSPKt   PROGBITS        00000000 001044 000004 00  AG  0   0  4
  [357] .rodata._ZTSPt    PROGBITS        00000000 001048 000003 00  AG  0   0  1
  [358] .rodata._ZTSt     PROGBITS        00000000 00104b 000002 00  AG  0   0  1
  [359] .rodata._ZTSPKs   PROGBITS        00000000 001050 000004 00  AG  0   0  4
  [360] .rodata._ZTSPs    PROGBITS        00000000 001054 000003 00  AG  0   0  1
  [361] .rodata._ZTSs     PROGBITS        00000000 001057 000002 00  AG  0   0  1
  [362] .rodata._ZTSPKh   PROGBITS        00000000 00105c 000004 00  AG  0   0  4
  [363] .rodata._ZTSPh    PROGBITS        00000000 001060 000003 00  AG  0   0  1
  [364] .rodata._ZTSh     PROGBITS        00000000 001063 000002 00  AG  0   0  1
  [365] .rodata._ZTSPKa   PROGBITS        00000000 001068 000004 00  AG  0   0  4
  [366] .rodata._ZTSPa    PROGBITS        00000000 00106c 000003 00  AG  0   0  1
  [367] .rodata._ZTSa     PROGBITS        00000000 00106f 000002 00  AG  0   0  1
  [368] .rodata._ZTSPKc   PROGBITS        00000000 001074 000004 00  AG  0   0  4
  [369] .rodata._ZTSPc    PROGBITS        00000000 001078 000003 00  AG  0   0  1
  [370] .rodata._ZTSc     PROGBITS        00000000 00107b 000002 00  AG  0   0  1
  [371] .rodata._ZTSPKDi  PROGBITS        00000000 001080 000005 00  AG  0   0  4
  [372] .rodata._ZTSPDi   PROGBITS        00000000 001088 000004 00  AG  0   0  4
  [373] .rodata._ZTSDi    PROGBITS        00000000 00108c 000003 00  AG  0   0  1
  [374] .rodata._ZTSPKDs  PROGBITS        00000000 001090 000005 00  AG  0   0  4
  [375] .rodata._ZTSPDs   PROGBITS        00000000 001098 000004 00  AG  0   0  4
  [376] .rodata._ZTSDs    PROGBITS        00000000 00109c 000003 00  AG  0   0  1
  [377] .rodata._ZTSPKw   PROGBITS        00000000 0010a0 000004 00  AG  0   0  4
  [378] .rodata._ZTSPw    PROGBITS        00000000 0010a4 000003 00  AG  0   0  1
  [379] .rodata._ZTSw     PROGBITS        00000000 0010a7 000002 00  AG  0   0  1
  [380] .rodata._ZTSPKb   PROGBITS        00000000 0010ac 000004 00  AG  0   0  4
  [381] .rodata._ZTSPb    PROGBITS        00000000 0010b0 000003 00  AG  0   0  1
  [382] .rodata._ZTSb     PROGBITS        00000000 0010b3 000002 00  AG  0   0  1
  [383] .rodata._ZTSPKv   PROGBITS        00000000 0010b8 000004 00  AG  0   0  4
  [384] .rodata._ZTSPv    PROGBITS        00000000 0010bc 000003 00  AG  0   0  1
  [385] .rodata._ZTSv     PROGBITS        00000000 0010bf 000002 00  AG  0   0  1
  [386] .data             PROGBITS        00001000 0010c4 000000 00  WA  0   0  4
  [387] .bss              NOBITS          00001000 0010c4 004500 00  WA  0   0  4
  [388] .bss._ZGVZN6syste NOBITS          00000000 0010c8 000008 00 WAG  0   0  8
  [389] .bss._ZZN6system7 NOBITS          00000000 0010c8 000008 00 WAG  0   0  4
  [390] .shstrtab         STRTAB          00000000 0010c8 000c48 00      0   0  1
  [391] .symtab           SYMTAB          00000000 006248 002350 10     392 348  4
  [392] .strtab           STRTAB          00000000 008598 000f97 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)
And here is my code:

Code: Select all

void* module_loader::open(void* in_memory)
{
    // Get byte array for module
    unsigned char* module = reinterpret_cast<unsigned char*>(in_memory);

    // Get ELF header
    Elf32_Ehdr* header = reinterpret_cast<Elf32_Ehdr*>(module);

    // Iterate through type-1 and type-8 sections to find module size,
    //  also find the symbol table.
    unsigned int module_size = 0;

    Elf32_Sym* symbol_table = nullptr;

    for (unsigned int offset = header->e_shoff, index = 0;
        index < header->e_shnum;
        offset += header->e_shentsize, ++index)
    {
        // Get section header
        Elf32_Shdr* section = reinterpret_cast<Elf32_Shdr*>(module + offset);

        // If type-1 or type-8 add the size to the total
        if (section->sh_type == 1 || section->sh_type == 8)
        {
            // Get size
            unsigned int section_size = section->sh_size;

            // Page-align it
            if ((section_size % 0x1000) != 0)
            {
                section_size = (section_size & 0xFFFFF000) + 0x1000;
            }

            // Add to total size
            module_size += section_size;
        }
        else if (section->sh_type == 2)
        {
            symbol_table = reinterpret_cast<Elf32_Sym*>(
                module + section->sh_offset);
        }
    }

    // Allocate pages
    unsigned char* memory = reinterpret_cast<unsigned char*>(
        indirect::allocator::instance().allocate(module_size / 0x1000));

    // Iterate through once more and place program bits in memory
    unsigned char* memory_last = memory;
    unsigned char* memory_current = memory;

    for (unsigned int offset = header->e_shoff, index = 0;
        index < header->e_shnum;
        offset += header->e_shentsize, ++index)
    {
        // Get section header
        Elf32_Shdr* section = reinterpret_cast<Elf32_Shdr*>(module + offset);

        switch (section->sh_type)
        {
        case 1: // Place program bits in memory
            {
                // Copy bits
                std::memcpy(memory_current + section->sh_addr,
                    module + section->sh_offset, section->sh_size);

                // Increment memory_current
                memory_last = memory_current;
                memory_current += section->sh_size;
            } break;

        case 8: // Null region ('.bss')
            {
                // Set memory
                memory_current = memory + section->sh_addr;

                // Null
                std::memset(memory_current, 0, section->sh_size);

                // Increment memory_current
                memory_last = memory_current;
                memory_current += section->sh_size;
            } break;

        case 9: // Apply relocations
            {
                // Get array
                Elf32_Rel* reloc = reinterpret_cast<Elf32_Rel*>(
                    module + section->sh_offset);

                Elf32_Rel* reloc_end = reinterpret_cast<Elf32_Rel*>(
                    module + section->sh_offset + section->sh_size);

                while (reloc < reloc_end)
                {
                    // Apply Relocation
                    unsigned int* address = reinterpret_cast<unsigned int*>(
                        memory_last + reloc->r_offset);

                    // Evaluate Relocation
                    switch (static_cast<unsigned char>(reloc->r_info))
                    {
                    case 1: // Symbol + Addend
                        {
                            (*address) =
                                symbol_table[reloc->r_info >> 8].st_value +
                                (*address);
                        } break;

                    case 2: // Symbol + Addend - Offset
                        {
                            (*address) =
                                symbol_table[reloc->r_info >> 8].st_value +
                                (*address) - reloc->r_offset;
                        } break;

                    default:
                        {
                            console::instance() << "Unhandled relocation type: "
                                << (unsigned int)(unsigned char)(reloc->r_info)
                                << '\n';
                        }
                    }

                    // Advance to next once
                    reloc = reinterpret_cast<Elf32_Rel*>(
                        reinterpret_cast<unsigned char*>(reloc) +
                        section->sh_entsize);
                }
            } break;
        }
    }

    // Call entry point
    unsigned int entry = reinterpret_cast<unsigned int>(memory) +
        header->e_entry;

    // Push begin and end for runtime, and call
    unsigned char* memory_end = memory + module_size;

    __asm__ ("movl %0, %%eax;"
             "movl %1, %%ebx;"
             : : "m" (memory_end), "m" (memory)
             : "%eax", "%ebx");

    reinterpret_cast<void (*)()>(entry)();

    // Return module memory address
    return reinterpret_cast<void*>(memory);
}
Thank you.

Re: What to do with ELF '.group' sections

Posted: Sat Feb 19, 2011 12:48 pm
by JamesM
They're used as hints to the linker.

You can ignore them.

Re: What to do with ELF '.group' sections

Posted: Sat Feb 19, 2011 1:14 pm
by blobmiester
Thanks.

That's about what I was thinking, and what I'm doing now. Any idea why my code doesn't work?

EDIT: Now I realize the several things I've done wrong. I'm not even checking the flag bits. I guess I missed a few pages in the ELF specs. Okay, I really shouldn't be programming while I'm sick, but what the hell. :D

EDIT:

Just wanted to pipe in and say that my elf loader and dynamic linker work awesomely now. :D This whole thing started because I got this really cool idea for a modular kernel. My other kernel (which is monolithic) is pretty far along but I haven't got to program loading yet (more or less skipped it to be honest, as I was more interested in other areas like vga). So this whole reading ELF thing is new (which only makes it more fun).

I basically threw out the first bit of code that I posted. I went back on the wiki and read the ELF page again. The section on relocation states you want to use the '-r' switch but that gives all kinds of section you'd have to deal with at runtime (when the linker is perfectly capable of doing statically for you). So I randomly typed in object files on the search, found the object files wiki page (which I've never seen before but what the hey) and on it, it says to use '-q'. Well, now I use '-q -shared' and that gives me the sections I need. Then I went back, re-read the ELF specs, and coded. :) One of the many reasons I just love programming (much more so osdev): that barely describable feeling you get when the piece of code you've been working on for the last several hours or more to actually work. :cry: :lol:

Anyway, that was rant-y. So, in brief: cool modular kernel idea, here I come.