This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Useful pieces of code needed

I've recently started learning how to program for my PhD and they say I'm very talented. So far I have written an equation program using simple components like 'if-else' and 'switch' functions.

I want to become better at programming, but the tutorials I have looked at don't give ways to use specific code pieces within programs.

For example, I would like to know how to set a switch other than to perform different functions. i.e "Choose from the options" and then set , and keys for each option. I presume this can be achieved using loops, something I don't fully understand yet.

Any other useful code that is often used in programming would also be appreciated.

Thank You

Parents
  • This is exactly what you want:

    It has for and switch, and it is not baby example.
    rswiki.csie.org/.../module.c

    1903 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
    1904    might -- code, read-only data, read-write data, small data.  Tally
    1905    sizes, and place the offsets into sh_entsize fields: high bit means it
    1906    belongs in init. */
    1907 static void layout_sections(struct module *mod, struct load_info *info)
    1908 {
    1909         static unsigned long const masks[][2] = {
    1910                 /* NOTE: all executable code must be the first section
    1911                  * in this array; otherwise modify the text_size
    1912                  * finder in the two loops below */
    1913                 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
    1914                 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
    1915                 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
    1916                 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
    1917         };
    1918         unsigned int m, i;
    1919
    1920         for (i = 0; i < info->hdr->e_shnum; i++)
    1921                 info->sechdrs[i].sh_entsize = ~0UL;
    1922
    1923         DEBUGP("Core section allocation order:\n");
    1924         for (m = 0; m < ARRAY_SIZE(masks); ++m) {
    1925                 for (i = 0; i < info->hdr->e_shnum; ++i) {
    1926                         Elf_Shdr *s = &info->sechdrs[i];
    1927                         const char *sname = info->secstrings + s->sh_name;
    1928
    1929                         if ((s->sh_flags & masks[m][0]) != masks[m][0]
    1930                             || (s->sh_flags & masks[m][1])
    1931                             || s->sh_entsize != ~0UL
    1932                             || strstarts(sname, ".init"))
    1933                                 continue;
    1934                         s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
    1935                         DEBUGP("\t%s\n", name);
    1936                 }
    1937                 switch (m) {
    1938                 case 0: /* executable */
    1939                         mod->core_size = debug_align(mod->core_size);
    1940                         mod->core_text_size = mod->core_size;
    1941                         break;
    1942                 case 1: /* RO: text and ro-data */
    1943                         mod->core_size = debug_align(mod->core_size);
    1944                         mod->core_ro_size = mod->core_size;
    1945                         break;
    1946                 case 3: /* whole core */
    1947                         mod->core_size = debug_align(mod->core_size);
    1948                         break;
    1949                 }
    1950         }
    1951
    1952         DEBUGP("Init section allocation order:\n");
    1953         for (m = 0; m < ARRAY_SIZE(masks); ++m) {
    1954                 for (i = 0; i < info->hdr->e_shnum; ++i) {
    1955                         Elf_Shdr *s = &info->sechdrs[i];
    1956                         const char *sname = info->secstrings + s->sh_name;
    1957
    1958                         if ((s->sh_flags & masks[m][0]) != masks[m][0]
    1959                             || (s->sh_flags & masks[m][1])
    1960                             || s->sh_entsize != ~0UL
    1961                             || !strstarts(sname, ".init"))
    1962                                 continue;
    1963                         s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
    1964                                          | INIT_OFFSET_MASK);
    1965                         DEBUGP("\t%s\n", sname);
    1966                 }
    1967                 switch (m) {
    1968                 case 0: /* executable */
    1969                         mod->init_size = debug_align(mod->init_size);
    1970                         mod->init_text_size = mod->init_size;
    1971                         break;
    1972                 case 1: /* RO: text and ro-data */
    1973                         mod->init_size = debug_align(mod->init_size);
    1974                         mod->init_ro_size = mod->init_size;
    1975                         break;
    1976                 case 3: /* whole init */
    1977                         mod->init_size = debug_align(mod->init_size);
    1978                         break;
    1979                 }
    1980         }
    1981 }
    

Reply
  • This is exactly what you want:

    It has for and switch, and it is not baby example.
    rswiki.csie.org/.../module.c

    1903 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
    1904    might -- code, read-only data, read-write data, small data.  Tally
    1905    sizes, and place the offsets into sh_entsize fields: high bit means it
    1906    belongs in init. */
    1907 static void layout_sections(struct module *mod, struct load_info *info)
    1908 {
    1909         static unsigned long const masks[][2] = {
    1910                 /* NOTE: all executable code must be the first section
    1911                  * in this array; otherwise modify the text_size
    1912                  * finder in the two loops below */
    1913                 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
    1914                 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
    1915                 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
    1916                 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
    1917         };
    1918         unsigned int m, i;
    1919
    1920         for (i = 0; i < info->hdr->e_shnum; i++)
    1921                 info->sechdrs[i].sh_entsize = ~0UL;
    1922
    1923         DEBUGP("Core section allocation order:\n");
    1924         for (m = 0; m < ARRAY_SIZE(masks); ++m) {
    1925                 for (i = 0; i < info->hdr->e_shnum; ++i) {
    1926                         Elf_Shdr *s = &info->sechdrs[i];
    1927                         const char *sname = info->secstrings + s->sh_name;
    1928
    1929                         if ((s->sh_flags & masks[m][0]) != masks[m][0]
    1930                             || (s->sh_flags & masks[m][1])
    1931                             || s->sh_entsize != ~0UL
    1932                             || strstarts(sname, ".init"))
    1933                                 continue;
    1934                         s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
    1935                         DEBUGP("\t%s\n", name);
    1936                 }
    1937                 switch (m) {
    1938                 case 0: /* executable */
    1939                         mod->core_size = debug_align(mod->core_size);
    1940                         mod->core_text_size = mod->core_size;
    1941                         break;
    1942                 case 1: /* RO: text and ro-data */
    1943                         mod->core_size = debug_align(mod->core_size);
    1944                         mod->core_ro_size = mod->core_size;
    1945                         break;
    1946                 case 3: /* whole core */
    1947                         mod->core_size = debug_align(mod->core_size);
    1948                         break;
    1949                 }
    1950         }
    1951
    1952         DEBUGP("Init section allocation order:\n");
    1953         for (m = 0; m < ARRAY_SIZE(masks); ++m) {
    1954                 for (i = 0; i < info->hdr->e_shnum; ++i) {
    1955                         Elf_Shdr *s = &info->sechdrs[i];
    1956                         const char *sname = info->secstrings + s->sh_name;
    1957
    1958                         if ((s->sh_flags & masks[m][0]) != masks[m][0]
    1959                             || (s->sh_flags & masks[m][1])
    1960                             || s->sh_entsize != ~0UL
    1961                             || !strstarts(sname, ".init"))
    1962                                 continue;
    1963                         s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
    1964                                          | INIT_OFFSET_MASK);
    1965                         DEBUGP("\t%s\n", sname);
    1966                 }
    1967                 switch (m) {
    1968                 case 0: /* executable */
    1969                         mod->init_size = debug_align(mod->init_size);
    1970                         mod->init_text_size = mod->init_size;
    1971                         break;
    1972                 case 1: /* RO: text and ro-data */
    1973                         mod->init_size = debug_align(mod->init_size);
    1974                         mod->init_ro_size = mod->init_size;
    1975                         break;
    1976                 case 3: /* whole init */
    1977                         mod->init_size = debug_align(mod->init_size);
    1978                         break;
    1979                 }
    1980         }
    1981 }
    

Children
No data